Web Development Articles

Page 472 of 801

Split one-dimensional array into two-dimensional array JavaScript

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

We are required to write a function that takes in a one-dimensional array as the first argument and a number n as the second argument and we have to make n subarrays inside of the parent array (if possible) and divide elements into them accordingly. If the array contains 9 elements and we asked to make 4 subarrays, then dividing 2 elements in each subarray creates 5 subarrays and 3 in each creates 3, so in such cases we have to fallback to nearest lowest level (3 in ...

Read More

Beginning and end pairs in array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 183 Views

We are required to write a JavaScript function that takes in an array of Number / String literals and returns another array of arrays. With each subarray containing exactly two elements, the nth element from start and nth from last. For example, if we have an array: const arr = [1, 2, 3, 4, 5, 6]; Then the output should be: const output = [[1, 6], [2, 5], [3, 4]]; How It Works The algorithm pairs elements from both ends of the array moving inward: ...

Read More

Sort array based on another array in JavaScript

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

We often need to sort an array based on the order specified in another array. This technique is useful when you want certain elements to appear first while maintaining the original order of remaining elements. For example, we have an original array and want elements from a sortOrder array to appear at the beginning: const originalArray = ['Apple', 'Cat', 'Fan', 'Goat', 'Van', 'Zebra']; const sortOrder = ['Zebra', 'Van']; Using Custom Sort Function We can create a custom comparator function that prioritizes elements present in the sortOrder array: const originalArray = ['Apple', 'Cat', ...

Read More

Random name generator function in JavaScript

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

We are required to write a JavaScript function that takes in a number n and returns a random string of length n containing no other than the 26 English lowercase alphabets. Example Let us write the code for this function: const num = 8; const randomNameGenerator = num => { let res = ''; for(let i = 0; i < num; i++){ const random = Math.floor(Math.random() * 26); res += String.fromCharCode(97 + ...

Read More

Filter an object based on an array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 836 Views

Let's say we have an array and an object like this: const arr = ['a', 'd', 'f']; const obj = { "a": 5, "b": 8, "c": 4, "d": 1, "e": 9, "f": 2, "g": 7 }; We are required to write a function that takes in the object and the array and filter away all the object properties that are not an element of the array. So, the output should only contain 3 properties, namely: "a", "d" and "f". Method 1: ...

Read More

Nearest Prime to a number - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 597 Views

We are required to write a JavaScript function that takes in a number and returns the first prime number that appears after n. For example: If the number is 24, then the output should be 29. Understanding Prime Numbers A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Examples include 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, etc. Implementation We'll create two functions: one to check if a number is prime, and another to find the nearest prime after a given ...

Read More

What should be the correct Algorithm to Get Array B from Array A counting backwards from the last element in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 145 Views

Consider the following binary array (Array A) − const arr = [1, 0, 1, 1, 1, 1, 0, 1, 1]; When this array is passed through the function, say sumRight(), it produces the following output array (Array B) − const output = [1, 0, 4, 3, 2, 1, 0, 2, 1]; Understanding the Algorithm Elements in array arr can be either 0 or 1. The function counts backward from the last element of array arr, if there are consecutive 1's in the array arr then the corresponding element in the output ...

Read More

JavaScript Return the lowest index at which a value should be inserted into an array once it has been sorted (either in ascending or descending order).

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 236 Views

We have to write a function that returns the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted (either in ascending or descending order). The returned value should be a number. For example, Let's say, we have a function getIndexToInsert() − getIndexToInsert([1, 2, 3, 4], 1.5, 'asc') should return 1 because it is greater than 1 (index 0), but less than 2 (index 1). Likewise, getIndexToInsert([20, 3, 5], 19, 'asc') should return 2 because once the array has been sorted in ...

Read More

How to remove every Nth element from an array JavaScript?

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

Removing every Nth element from an array is a common task in JavaScript. This can be accomplished using the Array.prototype.splice() method to modify the array in-place, or by creating a new array with the filtered elements. Method 1: Using splice() (In-Place Modification) The splice() method removes elements from the array and modifies the original array: const arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']; console.log("Original array:", arr); const removeNth = (arr, n) => { // Start from n-1 index and remove every nth element ...

Read More

Returning poker pair cards - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 206 Views

We are required to write a function that takes in an array of exactly five elements representing the five cards of a poker player drawn randomly. If the five cards contain at least one pair, our function should return the card number of the highest pair (trivial if there only exists a single pair). Else our function should return false. For example: If the array is − const arr = ['A', 'Q', '3', 'A', 'Q']; Then our function should return − 'A' (as 'A' > 'Q' in card games) Card Ranking System ...

Read More
Showing 4711–4720 of 8,010 articles
« Prev 1 470 471 472 473 474 801 Next »
Advertisements