We are required to write a JavaScript function that takes in any number of JavaScript arrays and returns one single array with all the values from input arrays concatenated into it.For example − If the input arrays are −[1, 5], [44, 67, 3], [2, 5], [7], [4], [3, 7], [6]Then the output should be −const output = [1, 5, 44, 67, 3, 2, 5, 7, 4, 3, 7, 6];ExampleFollowing is the code −const a = [1, 5], b = [44, 67, 3], c = [2, 5], d = [7], e = [4], f = [3, 7], g = [6]; const concatArrays = (...arr) => { const res = arr.reduce((acc, val) => { return acc.concat(...val); }, []); return res; }; console.log(concatArrays(a, b, c, d, e, f, g));OutputFollowing is the output in the console −[ 1, 5, 44, 67, 3, 2, 5, 7, 4, 3, 7, 6 ]
We are required to write a JavaScript function that takes in two numbers, say m and n, and it returns an array of first n multiples of m.For example − If the numbers are 4 and 6Then the output should be −const output = [4, 8, 12, 16, 20, 24]ExampleFollowing is the code −const num1 = 4; const num2 = 6; const multiples = (num1, num2) => { const res = []; for(let i = num1; i
A tidy number is a number whose digits are in non-decreasing order. We are required to write a JavaScript function that takes in a number and checks whether its a tidy number or not.For example −489 is a tidy number 234557 is also a tidy number 34535 is not a tidy numberExampleFollowing is the code −const num = 234789; const isTidy = (num, last = 10) => { if(num){ if(num % 10 > last){ return false; }; return isTidy(Math.floor(num / 10), (num % 10)); }; return true; }; console.log(isTidy(num));OutputFollowing is the output in the console −true
Suppose, we have an array of literals like this −const arr = [3, 5, 5, 2, 23, 4, 7, 8, 8, 9];We are required to write a JavaScript function that takes in this array and a number, say n, and returns an object representing the count of elements greater than and smaller than n.ExampleFollowing is the code −const arr = [3, 5, 5, 2, 23, 4, 7, 8, 8, 9]; const greaterSmallerNumbers = (arr, num) => { return arr.reduce((acc, val) => { let { greater, smaller } = acc; if(val > num){ greater++; }; if(val < num){ smaller++; }; return { greater, smaller }; }, { greater: 0, smaller: 0 }); }; console.log(greaterSmallerNumbers(arr, 3));OutputFollowing is the output in the console −{ greater: 8, smaller: 1 }
Suppose, we have an array of literals like this −const arr = [3, 5, 5, 2, 23, 4, 7, 8, 8, 9];We are required to write a JavaScript function that takes in such an array and a number, say n (n must be less than or equal to the length of array). And the function should reverse the first n elements of the array within.For example −If for this array, the number is 4 −const arr = [3, 5, 5, 2, 23, 4, 7, 8, 8, 9];Then the output should be −const output = [2, 5, 5, 3, 23, 4, ... Read More
We are given two arrays say, arr1 and arr2 of positive Numbers. The number of values in both the arrays are the same.We are required to write a function that finds the maximum sum of products of their elements.Each element in arr1 has to be multiplied with exactly one element in arr2 and vice versa such that each element of both the arrays appears exactly once and the sum of product produced is maximum.For example: if, arr1 = [5, 1, 3, 4, 2] and, arr2 = [8, 10, 9, 7, 6]Then a possible sum of product is −5*6 + 1*7 ... Read More
We are required to write a JavaScript function that takes in three unsorted numbers and returns the middlemost of them using minimum number of comparisons.For example: If the numbers are −34, 45, 12Then our function should return the following −34ExampleFollowing is the code −const num1 = 34; const num2 = 45; const num3 = 12; const middleOfThree = (a, b, c) => { // x is positive if a is greater than b. // x is negative if b is greater than a. x = a - b; y = b - c; z = ... Read More
We are required to write a JavaScript function that takes in an array of numbers and returns a subarray of two elements from the original array whose sum is closest to 0.If the length of the array is less than 2, we should return the whole array.For example: If the input array is −const arr = [4, 4, 12, 3, 3, 1, 5, -4, 2, 2];Here, the sum of pair [5, -4] is 1 which is closest 0 for any two adjacent elements of the array, so we should return [5, -4]ExampleFollowing is the code −const arr = [4, 4, ... Read More
We are required to write a JavaScript function that takes in an array of numbers of length N (N should be even) and divides the array into two sub-array (left and right) containing N/2 elements each and do the sum of the subarrays and then multiply both the subarrays.For example: If the input array is −const arr = [1, 2, 3, 4]Then the output should be −(2+1) * (3+4) = 21ExampleFollowing is the code −const arr = [1, 2, 3, 4] const subArrayProduct = arr => { const { length: l } = arr; const creds = arr.reduce((acc, ... Read More
We are required to write a JavaScript function that takes in one such array and counts the sum of all distinct elements of the array.For example: Suppose, we have an array of numbers like this −const arr = [1, 5, 2, 1, 2, 3, 4, 5, 7, 8, 7, 1];The output for the array mentioned above will be 20.ExampleFollowing is the code −const arr = [1, 5, 2, 1, 2, 3, 4, 5, 7, 8, 7, 1]; const distinctSum = arr => { let res = 0; for(let i = 0; i < arr.length; i++){ if(i === arr.lastIndexOf(arr[i])){ res += arr[i]; }; continue; }; return res; }; console.log(distinctSum(arr));OutputFollowing is the output in the console −30