Javascript Articles

Page 26 of 534

Merging two sorted arrays into one sorted array using JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 2K+ Views

ProblemWe are required to write a JavaScript function that takes in two sorted arrays of numbers our function should merge all the elements of both the arrays into a new array and return that new array sorted in the same order.ExampleFollowing is the code −const arr1 = [1, 3, 4, 5, 6, 8]; const arr2 = [4, 6, 8, 9, 11]; const mergeSortedArrays = (arr1 = [], arr2 = []) => {    const res = [];    let i = 0;    let j = 0;    while(i < arr1.length && j < arr2.length){       if(arr1[i] < ...

Read More

Finding all solutions of a Diophantine equation using JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 308 Views

ProblemWe are required to write a JavaScript function that takes in a number n. Our function should find all such number x and y such that −x^2 - 4y^2 = n.And it should return an array of all such pairs.ExampleFollowing is the code −const num = 90005; const findSolution = (num = 1) => {    const res = [];    let a, b;    for(let a = 1; a

Read More

Finding one missing number in a scrambled sequence using JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 309 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers containing numbers from 1 to n.The problem is that one number from the array goes missing and the array is not sorted as well. Our function should find and return that one number missing from the array.ExampleFollowing is the code −const arr = [4, 7, 1, 8, 9, 5, 2, 3]; const findMissing = (arr = []) => {    const sumArr = arr.reduce((acc, val) => acc + val);    const { length: len } = arr;    const sumFirst = (len + 1) * (len + 2) * .5;    const missing = sumFirst - sumArr;    return missing; }; console.log(findMissing(arr));Output6

Read More

Integers have sum of squared divisors as perfect square in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 326 Views

ProblemWe are required to write a JavaScript function that takes in a range specified by an array of two numbers m and n.Our function is supposed to find all integers between m and n (m and n integers such as 1

Read More

Finding the length of the diagonal of a cuboid using JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 265 Views

ProblemWe are required to write a JavaScript function that takes in the length, width and height of a cuboid and return the length of its diagonal.ExampleFollowing is the code −const height = 10; const width = 12; const length = 15; const findDiagonal = (l, w, h) => {    const ll = l * 2;    const ww = w * 2;    const hh = h * 2;    const sum = ll + ww + hh;    const diagonal = Math.sqrt(sum);    return diagonal; }; console.log(findDiagonal(length, width, height));Output8.602325267042627

Read More

Volume difference of cuboids in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 157 Views

ProblemWe are required to write a JavaScript function that takes in two arrays, specifying the lengths, widths, and heights of two cuboids.Our function should calculate the volume of both cuboids and return their absolute difference.ExampleFollowing is the code −const h1 = 10; const w1 = 12; const l1 = 15; const h2 = 12; const w2 = 15; const l2 = 9; const findVolumeDifference = (l1, w1, h1, l2, w2, h2) => {    const v1 = l1 * w1 * h1;    const v2 = l2 * w2 * h2;    const diff = Math.abs(v1 - v2);    return diff; }; console.log(findVolumeDifference(l1, w1, h1, l2, w2, h2));Output180

Read More

Squared and square rooted sum of numbers of an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 445 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers. Our function should take each number in the array and square it if it is even, or square root the number if it is odd and then return the sum of all the new numbers rounded to two decimal places.ExampleFollowing is the code −const arr = [45, 2, 13, 5, 14, 1, 20]; const squareAndRootSum = (arr = []) => {    const res = arr.map(el => {       if(el % 2 === 0){          return el * el;       }else{          return Math.sqrt(el);       };    });    const sum = res.reduce((acc, val) => acc + val);    return sum; }; console.log(squareAndRootSum(arr));Output613.5498231854631

Read More

Turn each character into its ASCII character code and join them together to create a number in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 409 Views

ProblemWe are required to write a JavaScript function that takes in a string. Our function should turn each character into its ASCII character code and join them together to create a number. Then we should replace all instances of 7 from this number to 1 to construct another number. Finally, we should return the difference of both these numbersExampleFollowing is the code −const str = 'AVEHDKDDS'; const ASCIIDifference = (str = '') => {    return str    .split('')    .map(c => c.charCodeAt(0))    .join('')    .split('')    .map(Number)    .filter(str => str === 7)    .length * 6; }; console.log(ASCIIDifference(str));Output12

Read More

Unique pairs in array that forms palindrome words in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 194 Views

ProblemWe are required to write a JavaScript function that takes in an array of unique words.Our function should return an array of all such index pairs, the words at which, when combined yield a palindrome word.ExampleFollowing is the code −const arr = ["abcd", "dcba", "lls", "s", "sssll"]; const findPairs = (arr = []) => {    const res = [];    for ( let i = 0; i < arr.length; i++ ){       for ( let j = 0; j < arr.length; j++ ){          if (i !== j ) {             let k = `${arr[i]}${arr[j]}`;             let l = [...k].reverse().join('');             if (k === l)             res.push( [i, j] );          }       };    };    return res; }; console.log(findPairs(arr));Output[ [ 0, 1 ], [ 1, 0 ], [ 2, 4 ], [ 3, 2 ] ]

Read More

Sorting according to number of 1s in binary representation using JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 207 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers. Our function should sort the numbers according to decreasing number of 1s present in the binary representation of those numbers and return the new array.ExampleFollowing is the code −const arr = [5, 78, 11, 128, 124, 68, 6]; const countOnes = (str = '') => {    let count = 0;    for(let i = 0; i < str.length; i++){       const el = str[i];       if(el === '1'){          count++;       };    };    return count; }; const sortByHighBit = (arr = []) => {    arr.sort((a, b) => countOnes(b) - countOnes(a));    return arr; }; console.log(sortByHighBit(arr));Output[ 5, 78, 11, 128, 124, 68, 6 ]

Read More
Showing 251–260 of 5,339 articles
« Prev 1 24 25 26 27 28 534 Next »
Advertisements