Front End Technology Articles

Page 198 of 652

Absolute difference of Number arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 513 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

Sum all perfect cube values upto n using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 338 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

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 315 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

Splitting number to contain continuous odd or even number using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 320 Views

Problem We need to write a JavaScript function that takes a positive number and splits it into continuous segments of odd or even digits. The function should create a new segment whenever it encounters a digit with different parity (odd becomes even or even becomes odd). Example For the number 124579: 1 (odd) - starts first segment 2, 4 (even) - starts second segment 5, 7, 9 (odd) - starts third segment Solution Following is the code: const num = 124579; const splitDifferent = (num = 1) => { const ...

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 232 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

Constructing a sentence based on array of words and punctuations using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 888 Views

We need to write a JavaScript function that takes an array of words and punctuations and constructs a proper sentence following specific spacing and punctuation rules. Problem Statement Our function should join array elements to construct a sentence based on the following rules: There must always be a space between words There must not be a space between a comma and the word on the left There must always be one and only one period at the end of a sentence Solution Here's how ...

Read More

Non-composite numbers sum in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 216 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 1971–1980 of 6,519 articles
« Prev 1 196 197 198 199 200 652 Next »
Advertisements