Javascript Arrays
Write a JavaScript program which prints the elements of the following array. Note : Use nested for loops.Sample array : var a = [[1, 2, 1, 24], [8, 11, 9, 4], [7, 0, 7, 27], [7, 4, 28, 14], [3, 10, 26, 7]]; Sample Output : "row 0" " 1" " 2" " 1" " 24" "row 1"------------
// a sample 2-D array var a = [[1, 2, 1, 24], [8, 11, 9, 4], [7, 0, 7, 27], [7, 4, 28, 14], [3, 10, 26, 7]]; for (var i in a) { console.log("row " + i); for (var j in a[i]) { console.log(" " + a[i][j]); } } Sample Output: row 0 1 2 1 24 row 1 8 11 9 4 row 2 7 0 7 27 row 3 7 4 28 14 row 4 3 10 26 7
Write a JavaScript function to create a specified number of elements and pre-filled numeric value array. Test Data: console.log(array_filled(6, 0)); [0, 0, 0, 0, 0, 0]console.log(array_filled(4, 11)); [11, 11, 11, 11]
function array_filled(n, val) { return Array.apply(null, Array(n)).map(Number.prototype.valueOf,val); } console.log(array_filled(6, 0)); console.log(array_filled(4, 11)); Sample Output: [0,0,0,0,0,0] [11,11,11,11]
Write a JavaScript function to create a specified number of elements and pre-filled string value array. Test Data: console.log(array_filled(3, 'default value')); ["default value", "default value", "default value"] console.log(array_filled(4, 'password')); ["password", "password", "password", "password"]
function array_filled(n, val) { return Array.apply(null, Array(n)).map(String.prototype.valueOf,val); } console.log(array_filled(3, 'default value')); console.log(array_filled(4, 'password')); Sample Output: ["default value","default value","default value"] ["password","password","password","password"]
Write a JavaScript function to generate an array of specified length, filled with integer numbers, increase by one from starting position. Test Data : console.log(array_range(1, 4));[1, 2, 3, 4] console.log(array_range(-6, 4));[-6, -5, -4, -3]
function array_range(start, len) { var arr = new Array(len); for (var i = 0; i < len; i++, start++) { arr[i] = start; } return arr; } console.log(array_range(1, 4)); console.log(array_range(-6, 4)); Sample Output: [1,2,3,4] [-6,-5,-4,-3]
Write a JavaScript function to move an array element from one position to another. Test Data: console.log(move([10, 20, 30, 40, 50], 0, 2)); [20, 30, 10, 40, 50] console.log(move([10, 20, 30, 40, 50], -1, -2)); [10, 20, 30, 50, 40]
function move(arr, old_index, new_index) { while (old_index < 0) { old_index += arr.length; } while (new_index < 0) { new_index += arr.length; } if (new_index >= arr.length) { var k = new_index - arr.length; while ((k--) + 1) { arr.push(undefined); } } arr.splice(new_index, 0, arr.splice(old_index, 1)[0]); return arr; } console.log(move([10, 20, 30, 40, 50], 0, 2)); console.log(move([10, 20, 30, 40, 50], -1, -2)); Sample Output: [20,30,10,40,50] [10,20,30,50,40]
Write a JavaScript function to get nth largest element from an unsorted array. Test Data: console.log(nthlargest([ 43, 56, 23, 89, 88, 90, 99, 652], 4)); 89
function nthlargest(arra,highest){ var x = 0, y = 0, z = 0, temp = 0, tnum = arra.length, flag = false, result = false; while(x < tnum){ y = x + 1; if(y < tnum){ for(z = y; z < tnum; z++){ if(arra[x] < arra[z]){ temp = arra[z]; arra[z] = arra[x]; arra[x] = temp; flag = true; }else{ continue; } } } if(flag){ flag = false; }else{ x++; if(x === highest){ result = true; } } if(result){ break; } } return (arra[(highest - 1)]); } console.log(nthlargest([ 43, 56, 23, 89, 88, 90, 99, 652], 4)); Sample Output: 89
Write a JavaScript program to count the number of arrays inside a given array. Test Data: ([2,8,[6],3,3,5,3,4,[5,4]]) -> 2 ([2,8,[6,3,3],[4],5,[3,4,[5,4]]]) -> 3
function test(arr_nums){ return arr_nums.filter(n=>Array.isArray(n)).length; } arr_nums = [2,8,[6],3,3,5,3,4,[5,4]] console.log("Number of arrays inside the said array: "+test(arr_nums)); arr_nums = [2,8,[6,3,3],[4],5,[3,4,[5,4]]] console.log("Number of arrays inside the said array: "+test(arr_nums)); Sample Output: Number of arrays inside the said array: 2 Number of arrays inside the said array: 3
Write a simple JavaScript program to join all elements of the following array into a string. Expected Output: "Red,Green,White,Black" "Red,Green,White,Black" "Red+Green+White+Black"
myColor = ["Red", "Green", "White", "Black"]; console.log(myColor.toString()); console.log(myColor.join()); console.log(myColor.join('+')); Red,Green,White,Black Red,Green,White,Black Red+Green+White+Black
Write a JavaScript program to flatten a nested (any depth) array. If you pass shallow, the array will only be flattened a single level. Sample Data: console.log(flatten([1, [2], [3, [[4]]],[5,6]])); [1, 2, 3, 4, 5, 6] console.log(flatten([1, [2], [3, [[4]]],[5,6]], true)); [1, 2, 3, [[4]], 5, 6]
var flatten = function(a, shallow,r){ if(!r){ r = []} if (shallow) { return r.concat.apply(r,a); } for(var i=0; i<a.length; i++){ if(a[i].constructor == Array){ flatten(a[i],shallow,r); }else{ r.push(a[i]); } } return r; } console.log(flatten([1, [2], [3, [[4]]],[5,6]])); console.log(flatten([1, [2], [3, [[4]]],[5,6]], true)); Sample Output: [1,2,3,4,5,6] [1,2,3,[[4]],5,6]
Write a JavaScript function to check whether an 'input' is an array or not. Test Data: console.log(is_array('w3resource'));console.log(is_array([1, 2, 4, 0])); false true ------------------------------------ HTML Code <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>JS Bin</title> </head> <body> </body> </html>
var is_array = function(input) { if (toString.call(input) === "[object Array]") return true; return false; }; console.log(is_array('w3resource')); console.log(is_array([1, 2, 4, 0])); Sample Output: false true
Write a JavaScript function to get the last element of an array. Passing a parameter 'n' will return the last 'n' elements of the array. Test Data : console.log(last([7, 9, 0, -2])); console.log(last([7, 9, 0, -2],3)); console.log(last([7, 9, 0, -2],6));
var last = function(array, n) { if (array == null) return void 0; if (n == null) return array[array.length - 1]; return array.slice(Math.max(array.length - n, 0)); }; console.log(last([7, 9, 0, -2])); console.log(last([7, 9, 0, -2],3)); console.log(last([7, 9, 0, -2],6)); -2 [9,0,-2] [7,9,0,-2]
Write a JavaScript program to remove all falsy values from an given object or array. -Use recursion. -Initialize the iterable data, using Array.isArray(), Array.prototype.filter() and Boolean for arrays in order to avoid sparse arrays. -Use Object.keys() and Array.prototype.reduce() to iterate over each key with an appropriate initial value. -Use Boolean to determine the truthiness of each key's value and add it to the accumulator if it's truthy. -Use typeof to determine if a given value is an object and call the function again to deeply compact it.
//Source:https://bit.ly/3hEZdCl const compactObject = val => { const data = Array.isArray(val) ? val.filter(Boolean) : val; return Object.keys(data).reduce( (acc, key) => { const value = data[key]; if (Boolean(value)) acc[key] = typeof value === 'object' ? compactObject(value) : value; return acc; }, Array.isArray(val) ? [] : {} ); }; const obj = { a: null, b: false, c: true, d: 0, e: 1, f: '', g: 'a', h: [null, false, '', true, 1, 'a'], i: { j: 0, k: false, l: 'a' } }; console.log(compactObject(obj)); Sample Output: {"c":true,"e":1,"g":"a","h":[true,1,"a"],"i":{"l":"a"}}
Write a JavaScript function to create an object from an array, using the specified key and excluding it from each value. Use Array.prototype.reduce() to create an object from arr. Use object destructuring to get the value of the given key and the associated data and add the key-value pair to the object.
//Source:https://bit.ly/3hEZdCl //Add the key-value pair to the object. const indexOn = (arr, key) => arr.reduce((obj, v) => { const { [key]: id, ...data } = v; obj[id] = data; return obj; }, {}); console.log(indexOn([ { id: 10, name: 'apple' }, { id: 20, name: 'orange' } ], x => x.id)); Sample Output: {"undefined":{"id":20,"name":"orange"}}
Write a JavaScript function to create an array of arrays, ungrouping the elements in an array produced by zip. Use Math.max(), Function.prototype.apply() to get the longest subarray in the array, Array.prototype.map() to make each element an array.Use Array.prototype.reduce() and Array.prototype.forEach() to map grouped values to individual arrays.
//Source:https://bit.ly/3hEZdCl //unzip const unzip = arr => arr.reduce( (acc, val) => (val.forEach((v, i) => acc[i].push(v)), acc), Array.from({ length: Math.max(...arr.map(x => x.length)) }).map(x => []) ); console.log(unzip([['a', 1, true], ['b', 2, false]])); console.log(unzip([['a', 1, true], ['b', 2]])); Sample Output: [["a","b"],[1,2],[true,false]] [["a","b"],[1,2],[true]]
Write a JavaScript program to generate all permutations of an array's elements (contains duplicates). -Use recursion. -For each element in the given array, create all the partial permutations for the rest of its elements. -Use Array.prototype.map() to combine the element with each partial permutation, then Array.prototype.reduce() to combine all permutations in one array. -Base cases are for Array.prototype.length equal to 2 or 1. -WARNING: This function's execution time increases exponentially with each array element. Anything more than 8 to 10 entries may cause your browser to hang as it tries to solve all the different combinations.
//Source:https://bit.ly/3hEZdCl const permutations = arr => { if (arr.length <= 2) return arr.length === 2 ? [arr, [arr[1], arr[0]]] : arr; return arr.reduce( (acc, item, i) => acc.concat( permutations([...arr.slice(0, i), ...arr.slice(i + 1)]).map(val => [ item, ...val, ]) ), [] ); }; console.log(permutations([1, 33, 5])); console.log(permutations([1, 3, 5, 7])); console.log(permutations([2, 4])); Sample Output: [[1,33,5],[1,5,33],[33,1,5],[33,5,1],[5,1,33],[5,33,1]] [[1,3,5,7],[1,3,7,5],[1,5,3,7],[1,5,7,3],[1,7,3,5],[1,7,5,3],[3,1,5,7],[3,1,7,5],[3,5,1,7],[3,5,7,1],[3,7,1,5],[3,7,5,1],[5,1,3,7],[5,1,7,3],[5,3,1,7],[5,3,7,1],[5,7,1,3],[5,7,3,1],[7,1,3,5],[7,1,5,3],[7,3,1,5],[7,3,5,1],[7,5,1,3],[7,5,3,1]] [[2,4],[4,2]]
Write a JavaScript program which accept a number as input and insert dashes (-) between each two even numbers. For example if you accept 025468 the output should be 0-254-6-8.
const num=window.prompt(); const str = num.toString(); const result = [str[0]]; for(let x=1; x<str.length; x++) { if((str[x-1]%2 === 0)&&(str[x]%2 === 0)) { result.push('-', str[x]); } else { result.push(str[x]); } } console.log(result.join('')); 0-254-6-8
Write a JavaScript program to find all unique values in an given array of numbers. Create a new Set() from the given array to discard duplicated values. Use the spread operator (...) to convert it back to an array
const unique_Elements = arr => [...new Set(arr)]; console.log(unique_Elements([1, 2, 2, 3, 4, 4, 5])); console.log(unique_Elements([1, 2, 3, 4, 5])); console.log(unique_Elements([1, -2, -2, 3, 4, -5, -6, -5])); Sample Output: [1,2,3,4,5] ] [1,2,3,4,5] [1,-2,3,4,-5,-6]
There are two arrays with individual values, write a JavaScript program to compute the sum of each individual index value from the given arrays. Sample array: array1 = [1,0,2,3,4]; array2 = [3,5,6,7,8,13]; Expected Output: [4, 5, 8, 10, 12, 13]
function Arrays_sum(array1, array2) { var result = []; var ctr = 0; var x=0; if (array1.length === 0) return "array1 is empty"; if (array2.length === 0) return "array2 is empty"; while (ctr < array1.length && ctr < array2.length) { result.push(array1[ctr] + array2[ctr]); ctr++; } if (ctr === array1.length) { for (x = ctr; x < array2.length; x++) { result.push(array2[x]); } } else { for (x = ctr; x < array1.length; x++) { result.push(array1[x]); } } return result; } console.log(Arrays_sum([1,0,2,3,4], [3,5,6,7,8,13])); Sample Output: [4,5,8,10,12,13]
Sample Output: Write a JavaScript program to perform a binary search. Note : A binary search or half-interval search algorithm finds the position of a specified input value within an array sorted by key value. Sample array: var items = [1, 2, 3, 4, 5, 7, 8, 9]; Expected Output: console.log(binary_Search(items, 1)); //0 console.log(binary_Search(items, 5)); //4
function binary_Search(items, value){ var firstIndex = 0, lastIndex = items.length - 1, middleIndex = Math.floor((lastIndex + firstIndex)/2); while(items[middleIndex] != value && firstIndex < lastIndex) { if (value < items[middleIndex]) { lastIndex = middleIndex - 1; } else if (value > items[middleIndex]) { firstIndex = middleIndex + 1; } middleIndex = Math.floor((lastIndex + firstIndex)/2); } return (items[middleIndex] != value) ? -1 : middleIndex; } var items = [1, 2, 3, 4, 5, 7, 8, 9]; console.log(binary_Search(items, 1)); console.log(binary_Search(items, 5)); Sample Output: 0 4
Write a JavaScript function to find an array contains a specific element. Test data: arr = [2, 5, 9, 6];console.log(contains(arr, 5)); [True]
function contains(arr, element) { for (var i = 0; i < arr.length; i++) { if (arr[i] === element) { return true; } } return false; } arr = [2, 5, 9, 6]; console.log(contains(arr, 5)); Sample Output: true
Write a JavaScript function to find the unique elements from two arrays. Test Data : console.log(difference([1, 2, 3], [100, 2, 1, 10])); ["1", "2", "3", "10", "100"] console.log(difference([1, 2, 3, 4, 5], [1, [2], [3, [[4]]],[5,6]])); ["1", "2", "3", "4", "5", "6"] console.log(difference([1, 2, 3], [100, 2, 1, 10])); ["1", "2", "3", "10", "100"]
function difference(arr1,arr2) { var a1= flatten(arr1,true); var a2= flatten(arr2,true); var a=[], diff=[]; for(var i=0;i< a1.length;i++) a[a1[i]]=false; for(i=0;i< a2.length;i++) if(a[a2[i]]===true) { delete a[a2[i]];} else a[a2[i]]=true; for(var k in a) diff.push(k); return diff; } var flatten = function(a, shallow,r){ if(!r){ r = [];} if (shallow) { return r.concat.apply(r,a); } for(i=0; i< a.length; i++){ if(a[i].constructor == Array){ flatten(a[i],shallow,r); }else{ r.push(a[i]); } } return r; }; console.log(difference([1, 2, 3], [100, 2, 1, 10])); console.log(difference([1, 2, 3, 4, 5], [1, [2], [3, [[4]]],[5,6]])); console.log(difference([1, 2, 3], [100, 2, 1, 10])); Sample Output: ["1","2","3","10","100"] ["1","2","3","4","5","6"] ["1","2","3","10","100"]
Write a JavaScript function to find the difference of two arrays. Test Data: console.log(difference([1, 2, 3], [100, 2, 1, 10]));["3", "10", "100"] console.log(difference([1, 2, 3, 4, 5], [1, [2], [3, [[4]]],[5,6]])); ["6"] console.log(difference([1, 2, 3], [100, 2, 1, 10])); ["3", "10", "100"]
function differenceOf2Arrays (array1, array2) { var temp = []; array1 = array1.toString().split(',').map(Number); array2 = array2.toString().split(',').map(Number); for (var i in array1) { if(array2.indexOf(array1[i]) === -1) temp.push(array1[i]); } for(i in array2) { if(array1.indexOf(array2[i]) === -1) temp.push(array2[i]); } return temp.sort((a,b) => a-b); } console.log(differenceOf2Arrays([1, 2, 3], [100, 2, 1, 10])); console.log(differenceOf2Arrays([1, 2, 3, 4, 5], [1, [2], [3, [[4]]],[5,6]])); Sample Output: [3,10,100] [6]
Write a JavaScript function to remove. 'null', '0', '""', 'false', 'undefined' and 'NaN' values from an array. Sample array: [NaN, 0, 15, false, -22, '',undefined, 47, null] Expected result: [15, -22, 47]
function filter_array(test_array) { var index = -1, arr_length = test_array ? test_array.length : 0, resIndex = -1, result = []; while (++index < arr_length) { var value = test_array[index]; if (value) { result[++resIndex] = value; } } return result; } console.log(filter_array([NaN, 0, 15, false, -22, '',undefined, 47, null])); Sample Output: [15,-22,47]
Write a JavaScript function to filter false, null, 0 and blank values from an array. Test Data : console.log(filter_array_values([58, '', 'abcd', true, null, false, 0]));[58, "abcd", true]
function filter_array_values(arr) { arr = arr.filter(isEligible); return arr; } function isEligible(value) { if(value !== false || value !== null || value !== 0 || value !== "") { return value; } } console.log(filter_array_values([58, '', 'abcd', true, null, false, 0])); Sample Output: [58,"abcd",true]
Write a JavaScript program to find duplicate values in a JavaScript array.
function find_duplicate_in_array(arra1) { var object = {}; var result = []; arra1.forEach(function (item) { if(!object[item]) object[item] = 0; object[item] += 1; }) for (var prop in object) { if(object[prop] >= 2) { result.push(prop); } } return result; } console.log(find_duplicate_in_array([1, 2, -2, 4, 5, 4, 7, 8, 7, 7, 71, 3, 6])); Sample Output: ["4","7"]
Find the leap years in a given range of years.
function leap_year_range(st_year, end_year) { var year_range = []; for (var i = st_year; i <= end_year; i++) { year_range.push(i); } var new_array = []; year_range.forEach( function(year) { if (test_LeapYear(year)) new_array.push(year); }); return new_array; } function test_LeapYear(year) { if ((year % 4 === 0 && year % 100 !== 0) || (year % 100 === 0 && year % 400 === 0)) { return year; } else { return false; } } console.log(leap_year_range(2000,2012)); Sample Output: [2000,2004,2008,2012]
Write a JavaScript function to find the longest common starting substring in a set of strings. Sample array : console.log(longest_common_starting_substring(['go', 'google'])); Expected result : "go"
function longest_common_starting_substring(arr1){ var arr= arr1.concat().sort(), a1= arr[0], a2= arr[arr.length-1], L= a1.length, i= 0; while(i< L && a1.charAt(i)=== a2.charAt(i)) i++; return a1.substring(0, i); } console.log(longest_common_starting_substring(['go', 'google'])); console.log(longest_common_starting_substring(['SQLInjection', 'SQLTutorial'])); console.log(longest_common_starting_substring(['abcd', '1234'])); Sample Output: go SQL
Write a JavaScript function to merge two arrays and removes all duplicates elements. Test data: var array1 = [1, 2, 3]; var array2 = [2, 30, 1]; console.log(merge_array(array1, array2)); [3, 2, 30, 1]
function merge_array(array1, array2) { var result_array = []; var arr = array1.concat(array2); var len = arr.length; var assoc = {}; while(len--) { var item = arr[len]; if(!assoc[item]) { result_array.unshift(item); assoc[item] = true; } } return result_array; } var array1 = [1, 2, 3]; var array2 = [2, 30, 1]; console.log(merge_array(array1, array2)); Sample Output: [3,2,30,1]
Write a JavaScript function to fill an array with values (numeric, string with one character) on supplied bounds. Test Data: console.log(num_string_range('a', "z", 2)); ["a", "c", "e", "g", "i", "k", "m", "o", "q", "s", "u", "w", "y"]
function num_string_range(start, end, step) { var range = []; if ((step === 0) || (typeof start == "undefined" || typeof end == "undefined") || (typeof start != typeof end)) return false; if (end < start) { step = -step; } if (typeof start == "number") { while (step > 0 ? end >= start : end <= start) { range.push(start); start += step; } } else if (typeof start == "string") { if (start.length != 1 || end.length != 1) { throw TypeError("Strings with one character are supported."); } start = start.charCodeAt(0); end = end.charCodeAt(0); while (step > 0 ? end >= start : end <= start) { range.push(String.fromCharCode(start)); start += step; } } else { throw TypeError("Only string and number are supported"); } return range; } console.log(num_string_range('a', "z", 2)); console.log(num_string_range("Z", "A", 2)); console.log(num_string_range(0, -5, 1)); console.log(num_string_range(0, 25, 5)); console.log(num_string_range(20, 5, 5)); Sample Output: ["a","c","e","g","i","k","m","o","q","s","u","w","y"] ["Z","X","V","T","R","P","N","L","J","H","F","D","B"] [0,-1,-2,-3,-4,-5] [0,5,10,15,20,25] [20,15,10,5]
Write a JavaScript function to get a random item from an array.
function random_item(items) { return items[Math.floor(Math.random()*items.length)]; } var items = [254, 45, 212, 365, 2543]; console.log(random_item(items)); Sample Output: 365
Sample Output: Write a JavaScript function to generate an array between two integers of 1 step length. Test Data : console.log(rangeBetwee(4, 7)); [4, 5, 6, 7]console.log(rangeBetwee(-4, 7)); [-4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7]
function rangeBetwee(start, end) { if (start > end) { var arr = new Array(start - end + 1); for (var i = 0; i < arr.length; i++, start--) { resarrult[i] = start; } return arr; } else { var arro = new Array(end-start+1); for (var j = 0; j < arro.length; j++, start++) { arro[j] = start; } return arro; } } console.log(rangeBetwee(4, 7)); console.log(rangeBetwee(-4, 7)); Sample Output: [4,5,6,7] [-4,-3,-2,-1,0,1,2,3,4,5,6,7]
Write a JavaScript program to remove duplicate items from an array (ignore case sensitivity).
function removeDuplicates(num) { var x, len=num.length, out=[], obj={}; for (x=0; x<len; x++) { obj[num[x]]=0; } for (x in obj) { out.push(x); } return out; } var Mynum = [1, 2, 2, 4, 5, 4, 7, 8, 7, 3, 6]; result = removeDuplicates(Mynum); console.log(Mynum); console.log(result); >>> Sample Solution-2: Removing duplicates from an array in JavaScript can be done in a variety of ways, such as using Array.prototype.reduce(), Array.prototype.filter() or even a simple for loop. But there's an easier alternative. JavaScript's built-in Set object is described as a collection of values, where each value may occur only once. A Set object is also iterable, making it easily convertible to an array using the spread (...) operator. JavaScript Code: //Source:https://bit.ly/3hEZdCl //Remove duplicates from a JavaScript array const nums = [1, 2, 2, 3, 1, 2, 4, 5, 4, 2, 6]; console.log([...new Set(nums)])
Write a JavaScript function to remove a specific element from an array. Test data: console.log(remove_array_element([2, 5, 9, 6], 5)); [2, 9, 6]
function remove_array_element(array, n) { var index = array.indexOf(n); if (index > -1) { array.splice(index, 1); } return array; } console.log(remove_array_element([2, 5, 9, 6], 5)); Sample Output: [2,9,6]
Write a JavaScript program to shuffle an array.
function shuffle(arra1) { var ctr = arra1.length, temp, index; // While there are elements in the array while (ctr > 0) { // Pick a random index index = Math.floor(Math.random() * ctr); // Decrease ctr by 1 ctr--; // And swap the last element with it temp = arra1[ctr]; arra1[ctr] = arra1[index]; arra1[index] = temp; } return arra1; } var myArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; console.log(shuffle(myArray)); Sample Output: [5,3,0,9,8,2,1,4,7,6]
Write a JavaScript program to find the sum of squares of a numeric vector.
function sum_sq(array) { var sum = 0, i = array.length; while (i--) sum += Math.pow(array[i], 2); return sum; } console.log(sum_sq([0,1,2,3,4]));
Write a JavaScript program that takes an array with mixed data type and calculate the sum of all numbers. Test Data: ([2, "11", 3, "a2", false, 5, 7, 1]) -> 18([2, 3, 0, 5, 7, 8, true, false]) -> 25
function test(arr_mix) { var total = 0; for(var i=0;i<=arr_mix.length;i++){ if (typeof arr_mix[i] === "number") total=total+arr_mix[i]; } return total; } arr_mix = [2, "11", 3, "a2", false, 5, 7, 1] console.log("Original array: "+arr_mix) console.log("Sum all numbers of the said array: "+test(arr_mix)); arr_mix = [2, 3, 0, 5, 7, 8, true, false] console.log("Original array: "+arr_mix) console.log("Sum all numbers of the said array: "+test(arr_mix)); Sample Output: Original array: 2,11,3,a2,false,5,7,1 Sum all numbers of the said array: 18 Original array: 2,3,0,5,7,8,true,false Sum all numbers of the said array: 25
Write a JavaScript program that takes an array of numbers and returns the third smallest number. Test Data: (2,3,5,7,1) -> 3(2,3,0,5,7,8,-2,-4) -> 0
function test(arr_nums) { return arr_nums.sort((x,y) => y - x)[arr_nums.length-3] } nums = [2, 3, 5, 7, 1] console.log("Original array of numbers: "+nums) console.log("Third smallest number of the said array of numbers: "+test(nums)); nums = [2, 3, 0, 5, 7, 8, -2, -4] console.log("Original array of numbers: "+nums) console.log("Third smallest number of the said array of numbers: "+test(nums)); Sample Output: Original array of numbers: 2,3,5,7,1 Third smallest number of the said array of numbers: 3 Original array of numbers: 2,3,0,5,7,8,-2,-4 Third smallest number of the said array of numbers: 0
Write a JavaScript program that takes an array of integers and returns false if every number is not prime. Otherwise, return true. Test Data: ([2,3,5,7]) -> true ([2,3,5,7,8]) -> false
function test(arr_nums) { for (n of arr_nums) { if (n == 1 | n > 2 & n % 2 == 0) return false; } return true; } nums = [2, 3, 5, 7] console.log("Original array of integers: "+nums) console.log("In the said array check every numbers are prime or not! "+test(nums)); nums = [2, 3, 5, 7, 8] console.log("Original array of integers: "+nums) console.log("In the said array check every numbers are prime or not! "+test(nums)); Sample Output: Original array of integers: 2,3,5,7 In the said array check every numbers are prime or not! true Original array of integers: 2,3,5,7,8 In the said array check every numbers are prime or not! false
Write a JavaScript program to get all the indexes where NaN is found of a given array of numbers and NaN. Test Data: ([2, NaN, 8, 16, 32]) -> [1] ([2, 4, NaN, 16, 32, NaN]) -> [2,5] ([2, 4, 16, 32]) ->[]
function test(arr_nums){ return arr_nums.map(function(el, i){ if(isNaN(el)) return i; return false; }).filter(function(el){ return el; }); } arr_nums = [2, NaN, 8, 16, 32] console.log("Original array: "+arr_nums) result = test(arr_nums) console.log("Find all indexes of NaN of the said array: "+result); arr_nums = [2, 4, NaN, 16, 32, NaN] console.log("Original array: "+arr_nums) result = test(arr_nums) console.log("Find all indexes of NaN of the said array: "+result); arr_nums = [2, 4, 16, 32] console.log("Original array: "+arr_nums) result = test(arr_nums) console.log("Find all indexes of NaN of the said array: "+result); Sample Output: Original array: 2,NaN,8,16,32 Find all indexes of NaN of the said array: 1 Original array: 2,4,NaN,16,32,NaN Find all indexes of NaN of the said array: 2,5 Original array: 2,4,16,32 Find all indexes of NaN of the said array:
Write a JavaScript program to check if an array is a factor chain or not. A factor chain is an array in which the previous element is a factor of the next consecutive element. The following is a factor chain:[2, 4, 8, 16, 32] // 2 is a factor of 4 // 4 is a factor of 8 // 8 is a factor of 16 // 16 is a factor of 32 Test Data: ([2, 4, 8, 16, 32]) -> true ([2, 4, 16, 32, 64]) -> true ([2, 4, 16, 32, 68]) -> false
function test(nums) { for (var i = 0; i < nums.length - 1; i++) { if (nums[i+1] % nums[i] != 0) { return false; } } return true; } nums = [2, 4, 8, 16, 32] console.log("Original array: ",nums) console.log("Check the said array is a factor chain or not?"); console.log(test(nums)); nums = [2, 4, 16, 32, 64] console.log("Original array: ",nums) console.log("Check the said array is a factor chain or not?"); console.log(test(nums)); nums = [2, 4, 16, 32, 68] console.log("Original array: ",nums) console.log("Check the said array is a factor chain or not?"); console.log(test(nums)); Sample Output: Original array: Check the said array is a factor chain or not? true Original array: Check the said array is a factor chain or not? true Original array: Check the said array is a factor chain or not? false
Write a JavaScript program to find a pair of elements (indices of the two numbers) from an given array whose sum equals a specific target number. Input: numbers= [10,20,10,40,50,60,70], target=50Output: 2, 3
function twoSum(nums, target_num) { var map = []; var indexnum = []; for (var x = 0; x < nums.length; x++) { if (map[nums[x]] != null) { var index = map[nums[x]]; indexnum[0] = index; indexnum[1] = x; break; } else { map[target_num - nums[x]] = x; } } return indexnum; } console.log(twoSum([10,20,10,40,50,60,70],50)); Sample Output: [2,3]
Write a JavaScript program to compute the union of two arrays. Sample Data: console.log(union([1, 2, 3], [100, 2, 1, 10])); [1, 2, 3, 10, 100]
function union(arra1, arra2) { if ((arra1 == null) || (arra2==null)) return void 0; var obj = {}; for (var i = arra1.length-1; i >= 0; -- i) obj[arra1[i]] = arra1[i]; for (var i = arra2.length-1; i >= 0; -- i) obj[arra2[i]] = arra2[i]; var res = []; for (var n in obj) { if (obj.hasOwnProperty(n)) res.push(obj[n]); } return res; } console.log(union([1, 2, 3], [100, 2, 1, 10])); Sample Output: [1,2,3,10,100]
Write a JavaScript script to empty an array keeping the original.
var arr = [1, 3, 6, 3, -5]; console.log(arr); arr.length = 0; console.log(arr); Sample Output: [1,3,6,3,-5] []
Write a JavaScript program to sort the items of an array. Sample array : var arr1 = [ -3, 8, 7, 6, 5, -4, 3, 2, 1 ]; Sample Output : -4,-3,1,2,3,5,6,7,8
var arr1=[-3,8,7,6,5,-4,3,2,1]; var arr2=[]; var min=arr1[0]; var pos; var max=arr1[0]; for (i=0; i<arr1.length; i++) { if (max<arr1[i]) max=arr1[i]; } for (var i=0;i<arr1.length;i++) { for (var j=0;j<arr1.length;j++) { if (arr1[j]!="x") { if (min>arr1[j]) { min=arr1[j]; pos=j; } } } arr2[i]=min; arr1[pos]="x"; min=max; } console.log(arr2);
Write a JavaScript program to find the most frequent item of an array. Sample array: var arr1=[3, 'a', 'a', 'a', 2, 3, 'a', 3, 'a', 2, 4, 9, 3]; Sample Output: a ( 5 times )
var arr1=[3, 'a', 'a', 'a', 2, 3, 'a', 3, 'a', 2, 4, 9, 3]; var mf = 1; var m = 0; var item; for (var i=0; i<arr1.length; i++) { for (var j=i; j<arr1.length; j++) { if (arr1[i] == arr1[j]) m++; if (mf<m) { mf=m; item = arr1[i]; } } m=0; } console.log(item+" ( " +mf +" times ) ") ;
Write a JavaScript program to compute the sum and product of an array of integers.
var array = [1, 2, 3, 4, 5, 6], s = 0, p = 1, i; for (i = 0; i < array.length; i += 1) { s += array[i]; p *= array[i]; } console.log('Sum : '+s + ' Product : ' +p);
Write a JavaScript function to clone an array. Test Data: console.log(array_Clone([1, 2, 4, 0])); console.log(array_Clone([1, 2, [4, 0]])); [1, 2, 4, 0][1, 2, [4, 0]]
var array_Clone = function(arra1) { return arra1.slice(0); }; console.log(array_Clone([1, 2, 4, 0])); console.log(array_Clone([1, 2, [4, 0]])); Sample Output: [1,2,4,0] [1,2,[4,0]]
Write a JavaScript program to display the colors in the following way. Here is the sample array: color = ["Blue ", "Green", "Red", "Orange", "Violet", "Indigo", "Yellow "]; o = ["th","st","nd","rd"] Output "1st choice is Blue ." "2nd choice is Green." "3rd choice is Red."
var color = ["Blue ", "Green", "Red", "Orange", "Violet", "Indigo", "Yellow "]; function Ordinal(n) { var o = ["th","st","nd","rd"], x = n%100; return x+(o[(x-20)%10]||o[x]||o[0]); } for(n = 0; n < color.length; n++){ var ordinal = n + 1; var output = (Ordinal(ordinal) + " choice is " + color[n] + "."); console.log(output); } Sample Output: 1st choice is Blue . 2nd choice is Green. 3rd choice is Red. 4th choice is Orange. 5th choice is Violet. 6th choice is Indigo. 7th choice is Yellow .
Write a JavaScript function to get the first element of an array. Passing a parameter 'n' will return the first 'n' elements of the array. Test Data: console.log(array_Clone([1, 2, 4, 0])); console.log(array_Clone([1, 2, [4, 0]])); [1, 2, 4, 0][1, 2, [4, 0]]
var first = function(array, n) { if (array == null) return void 0; if (n == null) return array[0]; if (n < 0) return []; return array.slice(0, n); }; console.log(first([7, 9, 0, -2])); console.log(first([],3)); console.log(first([7, 9, 0, -2],3)); console.log(first([7, 9, 0, -2],6)); console.log(first([7, 9, 0, -2],-3)); Sample Output: 7 [] [7,9,0] [7,9,0,-2] []
Write a JavaScript function to sort the following array of objects by title value. Sample object: var library = [ { author: 'Bill Gates', title: 'The Road Ahead', libraryID: 1254}, { author: 'Steve Jobs', title: 'Walter Isaacson', libraryID: 4264}, { author: 'Suzanne Collins', title: 'Mockingjay: The Final Book of The Hunger Games', libraryID: 3245} ];
var library = [ { author: 'Bill Gates', title: 'The Road Ahead', libraryID: 1254}, { author: 'Steve Jobs', title: 'Walter Isaacson', libraryID: 4264}, { author: 'Suzanne Collins', title: 'Mockingjay: The Final Book of The Hunger Games', libraryID: 3245} ]; function compare_to_sort(x,y) { if (x.title < y.title) return -1; if (x.title > y.title) return 1; return 0; } console.log(library.sort(compare_to_sort)); Sample Output: [ {"author":"Suzanne Collins","title":"Mockingjay: The Final Book of The Hunger Games","libraryID":3245},{"author":"Bill Gates","title":"The Road Ahead","libraryID":1254}, {"author":"Steve Jobs","title":"Walter Isaacson","libraryID":4264} ]
Write a JavaScript program which accept a string as input and swap the case of each character. For example if you input 'The Quick Brown Fox' the output should be 'tHE qUICK bROWN fOX'.
var str = 'c'; var UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; var LOWER = 'abcdefghijklmnopqrstuvwxyz'; var result = []; for(var x=0; x<str.length; x++) { if(UPPER.indexOf(str[x]) !== -1) { result.push(str[x].toLowerCase()); } else if(LOWER.indexOf(str[x]) !== -1) { result.push(str[x].toUpperCase()); } else { result.push(str[x]); } } console.log(result.join(''));
Write a JavaScript program to add items in an blank array and display the items.
var x = 0; var array = Array(); function add_element_to_array() { array[x] = document.getElementById("text1").value; alert("Element: " + array[x] + " Added at index " + x); x++; document.getElementById("text1").value = ""; } function display_array() { var e = "<hr/>"; for (var y=0; y<array.length; y++) { e += "Element " + y + " = " + array[y] + "<br/>"; } document.getElementById("Result").innerHTML = e; }
