Sorting Array Without Using Sort in JavaScript

AmitDiwan
Updated on 22-Oct-2020 12:59:41

7K+ Views

We are required to write a JavaScript function that takes in an array of numbers.The function should sort the array using the Array.prototype.sort() method, but, here, we are required to use the Array.prototype.reduce() method to sort the array.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [4, 56, 5, 3, 34, 37, 89, 57, 98]; const sortWithReduce = arr => {    return arr.reduce((acc, val) => {       let ind = 0;       while(ind < arr.length && val < arr[ind]){          ind++;       }       acc.splice(ind, 0, val);       return acc;    }, []); }; console.log(sortWithReduce(arr));OutputThe output in the console will be −[    98, 57, 89, 37, 34,    5, 56, 4, 3 ]

Squared Concatenation of a Number in JavaScript

AmitDiwan
Updated on 22-Oct-2020 12:57:07

246 Views

We are required to write a JavaScript function that takes in a number and returns a new number in which all the digits of the original number are squared and concatenated.For example: If the number is −99Then the output should be −8181because 9^2 is 81 and 1^2 is 1.Therefore, let’s write the code for this function −ExampleThe code for this will be −const num = 9119; const squared = num => {    const numStr = String(num);    let res = '';    for(let i = 0; i < numStr.length; i++){       const square = Math.pow(+numStr[i], 2);   ... Read More

Repeated Sum of Number's Digits in JavaScript

AmitDiwan
Updated on 22-Oct-2020 12:54:30

348 Views

We are required to write a JavaScript function that recursively sums up the digits of a number until it reduces to a single digit number.We are required to do so without converting the number to String or any other data type.Therefore, let’s write the code for this function −ExampleThe code for this will be −const num = 546767643; const sumDigit = (num, sum = 0) => {    if(num){       return sumDigit(Math.floor(num / 10), sum + (num % 10));    }    return sum; }; const sumRepeatedly = num => {    while(num > 9){       num = sumDigit(num);    };    return num; }; console.log(sumRepeatedly(num));OutputThe output in the console will be −3

Taking Common Elements from Many Arrays in JavaScript

AmitDiwan
Updated on 22-Oct-2020 12:50:49

522 Views

We are required to write a JavaScript function that takes in any arbitrary number of arrays and returns an array of elements that are common to all arrays. If there are no common elements, then we should return an empty array.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr1 = [2, 6, 7, 1, 7, 8, 4, 3]; const arr2 = [5, ,7, 2, 2, 1, 3]; const arr3 = [1, 56, 345, 6, 54, 2, 68, 85, 3]; const intersection = (arr1, arr2) => {    const res = [];    for(let ... Read More

Sum Digits and Find Nearest Prime in JavaScript

AmitDiwan
Updated on 22-Oct-2020 12:49:13

112 Views

We are required to write a JavaScript function that takes in a number, finds the sum of its digits and returns a prime number that is just greater than or equal to the sum.Therefore, let’s write the code for this function −ExampleThe code for this will be −const num = 56563; const digitSum = (num, sum = 0) => {    if(num){       return digitSum(Math.floor(num / 10), sum + (num % 10));    }    return sum; }; const isPrime = n => {    if (n===1){       return false;    }else if(n === 2){   ... Read More

Find Smallest Element from a String Array in JavaScript

AmitDiwan
Updated on 22-Oct-2020 12:46:07

295 Views

We are required to write a JavaScript function that takes in an array of strings and returns the index of string that is shortest in length.We will simply use a for loop and persist the index of string which is shortest in length.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = ['this', 'can', 'be', 'some', 'random', 'sentence']; const findSmallest = arr => {    const creds = arr.reduce((acc, val, index) => {       let { ind, len } = acc;       if(val.length < len){         ... Read More

Adding Up Identical Elements in JavaScript

AmitDiwan
Updated on 21-Oct-2020 12:42:29

99 Views

We are required to write a JavaScript function that takes in an array of Numbers and sums all the identical numbers together at one index.For exampleIf the input array is −const arr = [20, 10, 15, 20, 15, 10];Then the output should be −const output = [40, 20, 30];Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [20, 10, 15, 20, 15, 10]; const addSimilar = arr => {    for(let i = 0; i < arr.length; i++){       while(i !== arr.lastIndexOf(arr[i])){          const ind = arr.lastIndexOf(arr[i]);          arr[i] += arr.splice(ind, 1)[0];       };    }; }; addSimilar(arr); console.log(arr);OutputThe output in the console will be −[ 40, 20, 30 ]

Merge Two Arrays in a Unique Way in JavaScript

AmitDiwan
Updated on 21-Oct-2020 12:41:09

146 Views

We are required to write a JavaScript function that takes in two arrays and merges the arrays taking elements alternatively from the arrays.For exampleIf the two arrays are −const arr1 = [4, 3, 2, 5, 6, 8, 9]; const arr2 = [2, 1, 6, 8, 9, 4, 3];Then the output should be −const output = [4, 2, 3, 1, 2, 6, 5, 8, 6, 9, 8, 4, 9, 3];Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr1 = [4, 3, 2, 5, 6, 8, 9]; const arr2 = [2, 1, 6, 8, 9, 4, 3]; const mergeAlernatively = (arr1, arr2) => {    const res = [];    for(let i = 0; i < arr1.length + arr2.length; i++){       if(i % 2 === 0){          res.push(arr1[i/2]);       }else{          res.push(arr2[(i-1)/2]);       };    };    return res; }; console.log(mergeAlernatively(arr1, arr2));OutputThe output in the console will be −[    4, 2, 3, 1, 2, 6,    5, 8, 6, 9, 8, 4,    9, 3 ]

Picking Out Uniques from an Array in JavaScript

AmitDiwan
Updated on 21-Oct-2020 12:37:34

129 Views

Suppose we have an array that contains duplicate elements like this −const arr = [1, 1, 2, 2, 3, 4, 4, 5];We are required to write a JavaScript function that takes in one such array and returns a new array. The array should only contain the elements that only appear once in the original array.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [1, 1, 2, 2, 3, 4, 4, 5]; const extractUnique = arr => {    const res = [];    for(let i = 0; i < arr.length; i++){   ... Read More

Non-Composite Numbers Sum in an Array in JavaScript

AmitDiwan
Updated on 21-Oct-2020 12:36:18

158 Views

We are required to write a JavaScript function that takes in an array of numbers.The function should return the sum of all the prime numbers present in the array.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [43, 6, 6, 5, 54, 81, 71, 56, 8, 877, 4, 4]; const isPrime = n => {    if (n===1){       return false;    }else if(n === 2){       return true;    }else{       for(let x = 2; x < n; x++){          if(n % ... Read More

Advertisements