Web Development Articles

Page 470 of 801

Convert 2D array to object using map or reduce in JavaScript

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

Converting a 2D array to an object is a common task in JavaScript. Let's say we have a two-dimensional array that contains data about people's ages. The data is given by the following 2D array: const data = [ ['Rahul', 23], ['Vikky', 27], ['Sanjay', 29], ['Jay', 19], ['Dinesh', 21], ['Sandeep', 45], ['Umesh', 32], ['Rohit', 28], ]; console.log(data); [ [ 'Rahul', 23 ], [ 'Vikky', 27 ], [ 'Sanjay', 29 ], ...

Read More

Odd even sort in an array - JavaScript

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

We are required to write a JavaScript function that takes in an array of numbers and sorts the array such that first all the even numbers appear in ascending order and then all the odd numbers appear in ascending order. For example: If the input array is − const arr = [2, 5, 2, 6, 7, 1, 8, 9]; Then the output should be − const output = [2, 2, 6, 8, 1, 5, 7, 9]; Approach The solution uses a custom comparator function that: Places even numbers ...

Read More

Convert array of arrays to array of objects grouped together JavaScript

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

Let's say, we have a two-dimensional array that contains data about some colors and fruits like this: const data = [ ['orange', 'fruit'], ['red', 'color'], ['green', 'color'], ['orange', 'color'], ['banana', 'fruit'], ['blue', 'color'], ['lemon', 'fruit'], ['mango', 'fruit'], ['lemon', 'color'], ]; console.log("Original data:", data); Original data: [ [ 'orange', 'fruit' ], [ 'red', 'color' ], [ 'green', 'color' ], [ 'orange', 'color' ], [ 'banana', 'fruit' ], ...

Read More

Digit distance of two numbers - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 409 Views

We are required to write a JavaScript function that takes in two numbers and returns their digit distance. What is Digit Distance? The digit distance of two numbers is the absolute sum of the difference between their corresponding digits from left to right. For example, if the numbers are: 345 678 Then the digit distance calculation will be: |3-6| + |4-7| + |5-8| = 3 + 3 + 3 = 9 Implementation Here's how to calculate digit distance in JavaScript: const num1 = 345; const num2 ...

Read More

Intersection of two arrays JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 751 Views

Finding the intersection of two arrays means finding elements that appear in both arrays, preserving the frequency of each element as it appears in both arrays. For example, if we have arr1 = [1, 2, 3, 1] and arr2 = [1, 3, 1], the intersection is [1, 3, 1] because element 1 appears twice in both arrays, and element 3 appears once in both. Problem Statement Given two arrays of numbers, we need to write a function that computes their intersection and returns an array containing the intersecting elements. Each element in the result should appear as ...

Read More

Find the middle element of an array using recursion JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 529 Views

We are required to write an array function, say findMiddle that returns the middlemost element of the array without accessing its length property and without using any kind of built-in loops. If the array contains an odd number of elements, we return the one, middlemost element, or if the array contains an even number of elements, we return an array of two middlemost elements. So, let's write the code for this function. As you've already guessed, we will be making use of recursion to find these elements. The code for the recursive function will be — How It ...

Read More

JavaScript - Find the smallest n digit number or greater

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 440 Views

We are required to write a JavaScript function that takes in a number as the first argument, say n, and an array of numbers as the second argument. The function should return the smallest n digit number which is a multiple of all the elements specified in the array. If there exist no such n digit element then we should return the smallest such element. For example: If the array is: const arr = [12, 4, 5, 10, 9] For both n = 2 and n = 3, the output should be 180 Example Following ...

Read More

Group values in array by two properties JavaScript

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

We have an array of objects like this: const arr = [ { value: 12, gap: 1 }, { value: 13, gap: 1 }, { value: 14, gap: 1 }, { value: 15, gap: 1 }, { value: 19, gap: 2 }, { value: 21, gap: 1 }, { value: 22, gap: 1 }, { value: 23, gap: 1 }, { value: ...

Read More

How to count the number of elements in an array below/above a given number (JavaScript)

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 262 Views

In JavaScript, we often need to analyze arrays and count elements based on certain conditions. This article shows how to count array elements that fall below or above a given threshold number. Consider we have an array of numbers like this: const array = [3.1, 1, 2.2, 5.1, 6, 7.3, 2.1, 9]; We need to write a function that counts how many elements are below and above a given number. For example, if the number is 5.25, there should be 5 elements below it: (3.1, 1, 2.2, 5.1, 2.1) And ...

Read More

Determining rightness of a triangle – JavaScript

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

We are required to write a JavaScript function that takes in three numbers say a, b and c representing the length of three sides of a triangle. The function should return true if those three sides represent a right-angle triangle, false otherwise. Right Angle Triangle A triangle is a right-angle triangle if one of the three angles in the triangle is 90 degrees. And one angle in the triangle is 90 degrees when the square of the longest side is equal to the sum of squares of the other two sides. This is known as the Pythagorean ...

Read More
Showing 4691–4700 of 8,010 articles
« Prev 1 468 469 470 471 472 801 Next »
Advertisements