Javascript Articles

Page 487 of 534

How to get the numbers which can divide all values in an array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 489 Views

We are required to write a JavaScript function that takes in an array of numbers and returns a number which can exactly divide all the numbers in the array. Let's say the following is our array: const arr = [4, 6, 34, 76, 78, 44, 34, 26, 88, 76, 42]; Understanding the Problem We need to find the common divisors that can divide all numbers in the array. This is essentially finding the Greatest Common Divisor (GCD) of all array elements and then finding all its divisors. Method 1: Finding All Common Divisors ...

Read More

How can we make an Array of Objects from n properties of n arrays in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 205 Views

When working with multiple arrays in JavaScript, you often need to combine them into an array of objects. This is useful for creating structured data from separate arrays of related information. Suppose we have two arrays of literals like these: const options = ['A', 'B', 'C', 'D']; const values = [true, false, false, false]; We need to create a JavaScript function that combines these arrays into a new array of objects like this: [ {opt: 'A', val: true}, {opt: 'B', val: false}, {opt: 'C', val: false}, ...

Read More

How to insert an element into all positions in an array using recursion - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 560 Views

We are required to declare a function, let's say insertAllPositions, which takes two arguments — an element, x, and an array, arr. Functions must return an array of arrays, with each array corresponding to arr with x inserted in a possible position. That is, if arr is the length N, then the result is an array with N + 1 arrays — For example, the result of insertAllPositions(10, [1, 2, 3]) should be — const output = [ [10, 1, 2, 3], [1, 10, 2, 3], ...

Read More

Filter one array with another array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 399 Views

In JavaScript, filtering one array based on another array is a common task. This technique is useful when you need to keep only certain elements from an array based on criteria stored in another array. Problem Setup Consider the following arrays: const main = [ {name: "Karan", age: 34}, {name: "Aayush", age: 24}, {name: "Ameesh", age: 23}, {name: "Joy", age: 33}, {name: "Siddarth", age: 43}, {name: "Nakul", age: 31}, ...

Read More

The Zombie Apocalypse case study - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 299 Views

A nasty zombie virus is spreading out in the digital cities. We work at the digital CDC and our job is to look over the city maps and tell which areas are contaminated by the zombie virus so the digital army would know where to drop the bombs. They are the new kind of digital zombies which can travel only in vertical and horizontal directions and infect only numbers same as them. We'll be given a two-dimensional array with numbers in it. For some mysterious reason patient zero is always found in north west area of the ...

Read More

Finding the length of a JavaScript object

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 268 Views

JavaScript objects don't have a built-in length property like arrays. However, there are several methods to find the number of properties in an object. Suppose we have an object like this: const obj = { name: "Ramesh", age: 34, occupation: "HR Manager", address: "Tilak Nagar, New Delhi", experience: 13 }; We need to count the number of properties in this object. Using Object.keys() (Recommended) The most common and reliable method is Object.keys(), which returns an array of the object's property names: ...

Read More

Sorting an array of objects by property values - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 436 Views

In JavaScript, you can sort an array of objects by property values using the Array.sort() method with a custom comparison function. This is particularly useful when working with data structures like product catalogs, user lists, or any collection of objects. Sample Data Let's work with this array of home objects: const homes = [ { "h_id": "3", "city": "Dallas", "state": "TX", ...

Read More

Capitalize letter in a string in order and create an array to store - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 147 Views

We are required to write a JavaScript function that takes in a string and turns it into a Mexican Wave i.e. resembling string produced by successive capital letters in every word. For example, if the string is: const str = 'edabit'; Then the output should be the following with successive single capital letters: const output = ["Edabit", "eDabit", "edAbit", "edaBit", "edabIt", "edabiT"]; Method 1: Using String Prototype Extension This approach extends the String prototype with a custom replaceAt method: const str = 'edabit'; const replaceAt = function(index, ...

Read More

Recursion problem Snail Trail in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 208 Views

The snail trail problem involves traversing a 2D array in a spiral pattern from outside to inside. Given a square matrix, we need to create a flat array by following the spiral path: right → down → left → up, repeating until we reach the center. Suppose, we have an array like this: const arr = [ [1, 2, 3, 4], [12, 13, 14, 5], [11, 16, 15, 6], [10, 9, 8, 7] ]; The array is bound to be a square matrix. We are required to ...

Read More

Find closest index of array in JavaScript

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

When working with arrays in JavaScript, you might need to find the index of the element that is numerically closest to a given target value. This is useful in scenarios like finding the nearest data point, closest price match, or similar proximity-based searches. Problem Statement Given an array of numbers and a target value, we need to find the index of the array element that has the smallest absolute difference from the target. const arr = [2, 42, 82, 122, 162, 202, 242, 282, 322, 362]; For example, if our target is 150, we ...

Read More
Showing 4861–4870 of 5,340 articles
« Prev 1 485 486 487 488 489 534 Next »
Advertisements