Web Development Articles

Page 474 of 801

How to convert an array into a complex array JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 372 Views

In JavaScript, converting a flat array into a complex nested array structure involves grouping elements based on specific conditions. A common use case is splitting an array into subarrays when the sum of consecutive elements exceeds a given threshold. Let's say we need to write a function that takes an array of numbers and a number n, where n is the maximum sum allowed for each subarray. The function should break the array into subarrays whenever the sum of consecutive elements would exceed n. Example Problem Given an array and a threshold value, we want to create ...

Read More

Implementing Priority Sort in JavaScript

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

We are required to write a JavaScript function that takes in two arrays of numbers, second being smaller in size than the first. Our function should return a sorted version of the first array (in increasing order) but put all the elements that are common in both arrays to the front. For example − If the two arrays are − const arr1 = [5, 4, 3, 2, 1]; const arr2 = [2, 3]; Then the output should be − [2, 3, 1, 4, 5] How It Works The priority ...

Read More

How to generate child keys by parent keys in array JavaScript?

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

Let's say, we have an array of objects representing a hierarchical structure where each object has an id, parent_id, and title: const arr = [ { id: 1, parent_id: 0, title: 'Movies' }, { id: 2, parent_id: 0, title: 'Music' }, { id: 3, parent_id: 1, title: 'Russian movies' }, { id: 4, parent_id: 2, title: 'Russian music' }, { id: 5, parent_id: 3, title: 'New' }, { id: 6, parent_id: 3, title: 'Top10' ...

Read More

Compare two arrays and get those values that did not match JavaScript

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

We have two arrays of literals that contain some common values, our job is to write a function that returns an array with all those elements from both arrays that are not common. For example − // if the two arrays are: const first = ['cat', 'dog', 'mouse']; const second = ['zebra', 'tiger', 'dog', 'mouse']; // then the output should be: const output = ['cat', 'zebra', 'tiger'] // because these three are the only elements that are not common to both arrays Let's write the code for this − We will spread the two ...

Read More

Finding mistakes in a string - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 542 Views

We are required to write a JavaScript function that takes in two strings. The first string is some mistyped string and the second string is the correct version of this string. We can assume that the two strings we are getting as argument will always have the same length. We have to return the number of mistakes that exist in the first string by comparing it character by character with the correct version. Approach The solution involves iterating through both strings simultaneously and comparing each character at the same position. When characters don't match, we increment our ...

Read More

Shift last given number of elements to front of array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 308 Views

In JavaScript, moving elements from the end of an array to the front is a common array manipulation task. This tutorial shows how to create a function that shifts the last n elements to the beginning of an array in-place. Problem Statement We need to create an array method reshuffle() that: Takes a number n (where n ≤ array length) Moves the last n elements to the front Modifies the array in-place Returns true on success, false if n exceeds array length Example Input and Output // Original array const arr = ["blue", ...

Read More

Frequency distribution of elements - JavaScript

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

We are required to write a JavaScript function that takes a string and returns an object representing the frequency distribution of each character in the string. const str = 'This string will be used to calculate frequency distribution'; We need to return an object that represents the frequency distribution of various characters present in the string. Example Following is the code: const str = 'This string will be used to calculate frequency distribution'; const frequencyDistribution = str => { const map = {}; ...

Read More

Array sum: Comparing recursion vs for loop vs ES6 methods in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 331 Views

When working with arrays in JavaScript, there are multiple ways to calculate the sum of all elements. Let's compare three popular approaches: recursion, traditional for loops, and ES6 methods like reduce(). To demonstrate performance differences, we'll test each method with a large number of iterations. This gives us insights into which approach performs best for array summation tasks. Recursive Approach The recursive method calls itself until it processes all array elements: const recursiveSum = (arr, len = 0, sum = 0) => { if(len < arr.length){ ...

Read More

Find distinct elements - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 185 Views

We are required to write a JavaScript function that takes in an array of literals, such that some array elements are repeated. We are required to return an array that contains elements that appear only once (not repeated). For example: If the array is: const arr = [9, 5, 6, 8, 7, 7, 1, 1, 1, 1, 1, 9, 8]; Then the output should be: const output = [5, 6]; Using indexOf() and lastIndexOf() The first approach uses indexOf() and lastIndexOf() to check if an element appears only once. If ...

Read More

Compare two objects in JavaScript and return a number between 0 and 100 representing the percentage of similarity

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 626 Views

When comparing objects in JavaScript, we often need to determine their similarity as a percentage. This is useful for data matching, filtering, or recommendation systems. Problem Overview Given two objects, we need to calculate their similarity percentage based on matching key-value pairs. The similarity is calculated by dividing the count of matching properties by the total properties in the smaller object. const a = { Make: "Apple", Model: "iPad", hasScreen: "yes", Review: "Great product!", }; const b = { ...

Read More
Showing 4731–4740 of 8,010 articles
« Prev 1 472 473 474 475 476 801 Next »
Advertisements