Javascript Articles

Page 454 of 534

Recursively loop through an array and return number of items with JavaScript?

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

We need to write a function that recursively searches through a nested array and counts how many times a specific value appears. This is useful when working with complex nested data structures. Problem Statement Given a nested array, we want to count all occurrences of a search term, including items in nested sub-arrays. const names = ["rakesh", ["kalicharan", "krishna", "rakesh", "james", ["michael", "nathan", "rakesh", "george"]]]; For the above array, searching for "rakesh" should return 3 because it appears once at the top level and twice in nested arrays. Recursive Solution Here's a ...

Read More

Write an algorithm that takes an array and moves all of the zeros to the end JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 346 Views

We need to write a function that moves all zeros in an array to the end while maintaining the order of non-zero elements. This algorithm should work in-place without using extra space. Problem Overview Given an array with mixed numbers including zeros, we want to push all zeros to the end while keeping other elements in their relative order. For example, [1, 0, 3, 0, 5] should become [1, 3, 5, 0, 0]. Method 1: Using splice() and push() This approach removes zeros when found and adds them to the end: const arr = ...

Read More

Is there any more efficient way to code this "2 Sum" Questions JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 318 Views

Our job is to write a function that solves the two-sum problem in at most linear time. Two Sum Problem Given an array of integers, we have to find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers that add up to the target, and if no two elements add up to the target, our function should return an empty array. Brute Force Approach - O(n²) The naive approach uses nested loops to check every pair of elements: const bruteForceTwoSum ...

Read More

Converting string to MORSE code in JavaScript

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

What is Morse Code? Morse code is a method used in telecommunications to encode text characters as standardized sequences of two different signal durations, called dots and dashes. Each letter of the alphabet has a unique pattern of dots (.) and dashes (-). To convert a string to Morse code in JavaScript, we need an object that maps all alphabets to their Morse code equivalents, then iterate through the input string to build the encoded result. Morse Code Map First, let's create an object containing all alphabet-to-Morse mappings: const morseCode = { ...

Read More

Return the largest array between arrays JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 382 Views

We have an array of arrays that contains some numbers, we have to write a function that takes in that array and returns the index of the subarray that has the maximum sum. If more than one subarray has the same maximum sum, we have to return the index of first such subarray. Problem Overview Given multiple arrays nested within a main array, we need to: Calculate the sum of each subarray Find which subarray has the largest sum Return the index of that subarray ...

Read More

Create a Calculator function in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 905 Views

We have to write a function, say calculator() that takes in one of the four characters (+, - , *, / ) as the first argument and any number of Number literals after that. Our job is to perform the operation specified as the first argument over those numbers and return the result. If the operation is multiplication or addition, we are required to perform the same operation with every element. But if the operation is subtraction or division, we have to consider the first element as neutral and subtract all other elements from it or divide it by ...

Read More

Filter away object in array with null values JavaScript

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

When working with arrays of objects, you often need to filter out objects with invalid or empty values. This is common when processing API responses or user data that may contain incomplete records. Let's say we have an array of employee objects, but some have empty strings, null, or undefined values for the name field. We need to filter out these invalid entries. Sample Data Here's our employee data with some invalid entries: let data = [{ "name": "Ramesh Dhiman", "age": 67, "experience": ...

Read More

Strictly increasing sequence JavaScript

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

Given a sequence of integers as an array, we have to determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array. For example: For sequence = [1, 3, 2, 1], the output should be function(sequence) = false. There is no one element in this array that can be removed in order to get a strictly increasing sequence. For sequence = [1, 3, 2], the output should be function(sequence) = true. You can remove 3 from the array to get the strictly increasing sequence [1, 2]. Alternately, ...

Read More

Check if values of two arrays are the same/equal in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 698 Views

We have two arrays of numbers, let's say āˆ’ [2, 4, 6, 7, 1] [4, 1, 7, 6, 2] Assume, we have to write a function that returns a boolean based on the fact whether or not they contain the same elements irrespective of their order. For example āˆ’ [2, 4, 6, 7, 1] and [4, 1, 7, 6, 2] should yield true because they have the same elements but ordered differently. Method 1: Using includes() and Length Check This approach checks if both arrays have the same length and every element from ...

Read More

Insert value in the middle of every value inside array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 409 Views

In JavaScript, you can insert operation objects between array elements to create alternating patterns. This technique is useful for building mathematical expressions or form builders. Problem Statement We have an array of numbers like this: const numbers = [1, 6, 7, 8, 3, 98]; console.log("Original array:", numbers); Original array: [ 1, 6, 7, 8, 3, 98 ] We need to transform this array into objects with "value" keys, and insert operation objects between them using +, -, *, / alternately. Expected Output Structure The final result should look like ...

Read More
Showing 4531–4540 of 5,340 articles
« Prev 1 452 453 454 455 456 534 Next »
Advertisements