Javascript Articles

Page 120 of 534

Deep Search JSON Object JavaScript

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

Deep searching a JSON object means recursively traversing through all nested objects and arrays to find elements that match specific criteria. This is useful when you need to locate objects buried deep within complex data structures. The Problem Suppose we have the following nested JSON object: const obj = { id: 1, title: 'hello world', child: { id: null, title: 'foobar', ...

Read More

Convert JSON to another JSON format with recursion JavaScript

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

Suppose, we have the following JSON object − const obj = { "context": { "device": { "localeCountryCode": "AX", "datetime": "3047-09-29T07:09:52.498Z" }, "currentLocation": { "country": "KM", ...

Read More

Sorting Array based on another array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 472 Views

Sorting an array based on another array's order is a common JavaScript task. This technique allows you to reorder elements according to a reference array's sequence. Problem Statement Given two arrays, we need to sort the first array based on the order of elements in the second array: const input = ['S-1', 'S-2', 'S-3', 'S-4', 'S-5', 'S-6', 'S-7', 'S-8']; const sortingArray = ["S-1", "S-5", "S-2", "S-6", "S-3", "S-7", "S-4", "S-8"]; console.log("Original array:", input); console.log("Reference order:", sortingArray); Original array: [ 'S-1', 'S-2', 'S-3', 'S-4', 'S-5', 'S-6', 'S-7', 'S-8' ] Reference order: [ 'S-1', ...

Read More

How to find and return the longest repeating series of numbers in array with JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 633 Views

We are required to write a JavaScript function that takes in an array of Numbers that may contain some repeating elements. The function should return the length of the longest repeating number sequence from the array. For example − If the input array is − const arr = [2, 1, 1, 2, 3, 3, 2, 2, 2, 1]; Then the output should be 3 because the number 2 is repeated 3 times consecutively in the array (and that's the highest number). Using Array.reduce() Method The reduce() method can be used to group ...

Read More

JavaScript function that takes a multidimensional and a single array, and finds matches of the single array in the multi-d array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 194 Views

We need to write a JavaScript function that takes a multidimensional array (array of arrays) as the first argument and a single array as the second argument. The function should find common elements between each subarray and the single array, returning a new array containing only the matching elements from each subarray. Problem Understanding For each subarray in the multidimensional array, we want to find elements that also exist in the single array. The result preserves the structure but only includes common elements. For example, if we have: const arr1 = [ ...

Read More

JavaScript group array - find the sets of numbers that can be traveled to using the edges defined

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 138 Views

Consider the following input and output arrays: const input = ["0:3", "1:3", "4:5", "5:6", "6:8"]; const output = [ [0, 1, 3], [4, 5, 6, 8] ]; Considering each number as a node in a graph, and each pairing x:y as an edge between nodes x and y, we are required to find the sets of numbers that can be traveled to using the edges defined. In graph theory terms, we need to find the distinct connected components within such a graph. For instance, in the above arrays, there ...

Read More

Reversing vowels in a string JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 437 Views

We are required to write a JavaScript function that takes a string as input and reverse only the vowels of a string. Problem Example If the input string is: const str = 'Hello'; Then the output should be: 'Holle' Notice how 'e' and 'o' swap positions while consonants 'H', 'l', 'l' remain in their original places. Solution Using Two Pointers The most efficient approach uses two pointers from opposite ends of the string: const str = 'Hello'; const reverseVowels = (str = '') => { ...

Read More

Finding square root of a non-negative number without using Math.sqrt() JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 396 Views

We are required to write a JavaScript function that takes in a non-negative integer and computes and returns its square root without using Math.sqrt(). We can floor off a floating-point number to an integer. For example: For the number 15, we need not return the precise value, we can just return the nearest smaller integer value that will be 3, in case of 15. We will make use of the binary search algorithm to converge to the square root of the given number. Binary Search Approach The binary search method works by maintaining a range [left, ...

Read More

Find the Length of the Longest possible palindrome string JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 461 Views

Given a string s which consists of lowercase or uppercase letters, we are required to return the length of the longest palindrome that can be built with those letters. Letters are case sensitive, for example, "Aa" is not considered a palindrome here. For example, if the input string is "abccccdd", the output should be 7 because one longest palindrome that can be built is "dccaccd", whose length is 7. Algorithm Explanation The key insight is that a palindrome can have pairs of characters (which appear on both sides) plus at most one character in the middle. We ...

Read More

Finding perfect numbers in JavaScript

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

A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. A divisor of an integer x is an integer that can divide x evenly. For example: 28 is a perfect number, because 28 = 1 + 2 + 4 + 7 + 14 We are required to write a JavaScript function that takes in a number, say n, and determines whether or not n is a perfect number. How Perfect Numbers Work To check if a number is perfect, we need to: ...

Read More
Showing 1191–1200 of 5,340 articles
« Prev 1 118 119 120 121 122 534 Next »
Advertisements