Javascript Articles

Page 132 of 534

Finding product of an array using recursion in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 679 Views

We are required to write a JavaScript function that takes in an array of numbers. Our function should do the following two things: Make use of a recursive approach. Calculate the product of all the elements in the array. And finally, it should return the product. For example, if the input array is: const arr = [1, 3, 6, 0.2, 2, 5]; Then the output should be: 36 How Recursion Works Recursion breaks down the problem into smaller subproblems. For ...

Read More

Find what numbers were pressed to get the word (opposite of phone number digit problem) in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 200 Views

The mapping of the numerals to alphabets in the old keypad type phones used to be like this: const mapping = { 1: [], 2: ['a', 'b', 'c'], 3: ['d', 'e', 'f'], 4: ['g', 'h', 'i'], 5: ['j', 'k', 'l'], 6: ['m', 'n', 'o'], 7: ['p', 'q', 'r', 's'], 8: ['t', 'u', 'v'], 9: ['w', 'x', 'y', 'z'] }; console.log(mapping); { '1': [], '2': [ 'a', 'b', 'c' ], '3': ...

Read More

Are the strings anagrams in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 226 Views

Two strings are said to be anagrams of each other if by rearranging, rephrasing or shuffling the letters of one string we can form the other string. Both strings must contain exactly the same characters with the same frequency. For example, 'something' and 'emosghtin' are anagrams of each other because they contain the same letters with the same count. We need to write a JavaScript function that takes two strings and returns true if they are anagrams of each other, false otherwise. Method 1: Character Frequency Count This approach counts the frequency of each character in ...

Read More

Subset with maximum sum in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 379 Views

In JavaScript, finding the subset of non-adjacent elements with maximum sum is a classic dynamic programming problem. We need to decide whether to include each element or skip it, ensuring no two adjacent elements are selected. The key insight is that for each element, we have two choices: include it (and skip the previous element) or exclude it (and take the maximum sum up to the previous element). Problem Statement Given an array of integers, find the subset of non-adjacent elements that produces the maximum sum. Adjacent elements cannot be selected together. For example, with the ...

Read More

Check if the string is a combination of strings in an array using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 259 Views

We are required to write a JavaScript function that takes in an array of strings as the first argument and a string as the second argument. The function should check whether the string specified by second argument can be formed by combining the strings of the array in any possible way. For example − If the input array is − const arr = ["for", "car", "keys", "forth"]; And the string is − const str = "forthcarkeys"; Then the output should be true, because the string is a combination of elements ...

Read More

How to build a string with no repeating character n separate list of characters? in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 466 Views

When working with multiple arrays of characters, we often need to create combinations where each string contains exactly one character from each array with no duplicate characters. This is useful for generating unique combinations while avoiding repeated letters that might exist across different arrays. Problem Overview Given multiple arrays of characters, we need to build all possible strings that: Contains exactly one letter from each array ...

Read More

Split number into 4 random numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 984 Views

We need to write a JavaScript function that takes a total number and a maximum value, then generates four random numbers that sum to the total while ensuring none exceeds the maximum. The function should generate four random numbers where: All four numbers sum to the given total No number exceeds the specified maximum Repetition of numbers is allowed For example, if the total is 10 and maximum is 4, a valid output could be [3, 2, 3, 2]. Algorithm Overview The algorithm works by: Generating random values between 0 and 1 Scaling ...

Read More

How to check if an array contains integer values in JavaScript ?

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

In JavaScript, checking if an array contains integer values requires understanding the difference between numbers and strings that look like numbers. This article explores different approaches to detect actual integer values in arrays. The Problem with String Numbers Arrays often contain string representations of numbers like "123" instead of actual numbers. We need to distinguish between these types. const mixedArray = ["123", 45, "hello", 67.5, "89"]; console.log(typeof "123"); // "string" console.log(typeof 45); // "number" string number Method 1: Using typeof and Number.isInteger() The most ...

Read More

How to convert nested array pairs to objects in an array in JavaScript ?

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

When working with complex data structures, you often need to convert nested array pairs into objects. This is common when processing form data or API responses that use key-value pair arrays. Problem Statement Suppose we have an array of arrays like this: const arr = [ [ ['firstName', 'Joe'], ['lastName', 'Blow'], ['age', 42], ['role', 'clerk'] ], ...

Read More

Sort an array of objects by multiple properties in JavaScript

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

Sorting an array of objects by multiple properties is a common requirement in JavaScript applications. This involves creating custom comparison logic that prioritizes different properties based on specific criteria. Suppose, we have an array of objects like this: const arr = [ { id: 1, score: 1, isCut: false, dnf: false }, { id: 2, score: 2, isCut: false, dnf: false }, { id: 3, score: 3, isCut: false, dnf: false }, { id: 4, score: 4, isCut: false, dnf: false ...

Read More
Showing 1311–1320 of 5,340 articles
« Prev 1 130 131 132 133 134 534 Next »
Advertisements