Web Development Articles

Page 199 of 801

Absolute difference of Number arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 511 Views

In JavaScript, calculating the absolute difference between corresponding elements of two arrays is a common operation. This involves subtracting elements at the same index positions and taking the absolute value of the result. Suppose we have two arrays like these: const arr1 = [1, 2, 3, 4, 5, 6]; const arr2 = [9, 8, 7, 5, 8, 3]; We need to create a function that returns an array of absolute differences between corresponding elements. For these arrays, the expected output should be: [8, 6, 4, 1, 3, 3] Using For Loop ...

Read More

Merging two sorted arrays into one sorted array using JavaScript

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

We are required to write a JavaScript function that takes in two sorted arrays of numbers. Our function should merge all the elements of both arrays into a new array and return that new array sorted in the same order. Two-Pointer Approach The most efficient approach uses two pointers to traverse both arrays simultaneously, comparing elements and adding the smaller one to the result array. const arr1 = [1, 3, 4, 5, 6, 8]; const arr2 = [4, 6, 8, 9, 11]; const mergeSortedArrays = (arr1 = [], arr2 = []) => { ...

Read More

Sort the second array according to the elements of the first array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 301 Views

Suppose, we have two arrays like these − const arr1 = ['d', 'a', 'b', 'c']; const arr2 = [{a:1}, {c:3}, {d:4}, {b:2}]; console.log("First array:", arr1); console.log("Second array:", arr2); First array: [ 'd', 'a', 'b', 'c' ] Second array: [ { a: 1 }, { c: 3 }, { d: 4 }, { b: 2 } ] We are required to write a JavaScript function that accepts these two arrays. The function should sort the second array according to the elements of the first array. We have to sort the keys of ...

Read More

Volume difference of cuboids in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 168 Views

We 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. Formula The volume of a cuboid is calculated as: Volume = length × width × height Example Following 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, ...

Read More

Removing all the empty indices from array in JavaScript

Nikhilesh Aleti
Nikhilesh Aleti
Updated on 15-Mar-2026 2K+ Views

When working with JavaScript arrays, you may encounter arrays with empty indices (holes). These empty slots can cause issues in data processing. This article shows different methods to remove empty indices and create clean arrays. Understanding Empty Indices Empty indices in arrays are undefined slots that exist but have no value assigned: Empty Indices Example const array = [22, 45, , 56, 71, , 10]; console.log("Original array:", array); ...

Read More

Function to compute factorial of a number in JavaScript

Nikhilesh Aleti
Nikhilesh Aleti
Updated on 15-Mar-2026 13K+ Views

In this article, we will explore different methods to calculate the factorial of a number in JavaScript. What is Factorial? The factorial of a non-negative integer n is the product of all positive integers less than or equal to n. For example, the factorial of 4 is 4 × 3 × 2 × 1 = 24, represented as 4!. The value of 0! is defined as 1 by convention. Mathematical Formula The factorial of n is represented as n! and calculated using: n! = n × (n-1) × (n-2) × ... × 3 ...

Read More

Return an array of all the indices of minimum elements in the array in JavaScript

Nikhilesh Aleti
Nikhilesh Aleti
Updated on 15-Mar-2026 1K+ Views

In JavaScript arrays, you might need to find all indices where the minimum value appears. This is useful when the smallest element occurs multiple times and you want to locate all positions. Problem Statement Given an array with duplicate minimum values, we need to return an array containing all indices of these minimum elements. Input: [10, 22, 30, 44, 10, 22, 30, 10, 10] Output: [0, 4, 7, 8] Here, 10 is the minimum value appearing at indices 0, 4, 7, and 8. Using Math.min() with Spread Operator Math.min() returns the smallest ...

Read More

Pushing false objects to bottom in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 228 Views

Suppose we have an array of objects like this − const array = [ {key: 'a', value: false}, {key: 'a', value: 100}, {key: 'a', value: null}, {key: 'a', value: 23} ]; We are required to write a JavaScript function that takes in one such array and places all the objects that have falsy values for the "value" property to the bottom and sorts all other objects in decreasing order by the "value" property. Understanding Falsy Values In JavaScript, falsy values include: false, ...

Read More

Sum all perfect cube values upto n using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 332 Views

Problem We are required to write a JavaScript function that takes in a number n and returns the sum of all perfect cube numbers smaller than or equal to n. Understanding Perfect Cubes Perfect cubes are numbers that can be expressed as n³ where n is an integer. For example: 1³ = 1, 2³ = 8, 3³ = 27, 4³ = 64, etc. Example Following is the code − const num = 23546; const sumPerfectCubes = (num = 1) => { let i = 1; let sum = 0; while(i * i * i

Read More

Non-composite numbers sum in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 210 Views

In JavaScript, you can calculate the sum of all prime numbers in an array by first checking each number for primality, then adding the prime numbers together. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Understanding Prime Numbers Prime numbers are numbers like 2, 3, 5, 7, 11, 13, etc. The number 1 is not considered prime, and 2 is the only even prime number. Creating a Prime Check Function First, we need a helper function to determine if a number is prime: ...

Read More
Showing 1981–1990 of 8,010 articles
« Prev 1 197 198 199 200 201 801 Next »
Advertisements