Javascript Articles

Page 75 of 534

Reduce an array to groups in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 249 Views

In JavaScript, you can group consecutive duplicate elements in an array using the reduce() method. This technique combines adjacent duplicate values while preserving the original order. Problem Statement Given an array with duplicate entries, we need to merge consecutive duplicate elements together: const arr = ['blue', 'blue', 'green', 'blue', 'yellow', 'yellow', 'green']; console.log("Input array:", arr); Input array: [ 'blue', 'blue', 'green', 'blue', 'yellow', 'yellow', 'green' ] The expected output should combine only consecutive duplicates: [ 'blueblue', 'green', 'blue', 'yellowyellow', 'green' ] Using Array.reduce() Method The reduce() ...

Read More

Loop through an index of an array to search for a certain letter in JavaScript

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

We need to write a JavaScript function that takes an array of strings and a single character, then returns true if the character exists in any string within the array, false otherwise. Problem Overview The function should search through each string in the array to find if the specified character exists anywhere. This is a common pattern for filtering or validation tasks. Example Implementation Here's how to implement this search functionality: const arr = ['first', 'second', 'third', 'last']; const searchForLetter = (arr = [], letter = '') => { ...

Read More

Reduce array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 242 Views

In JavaScript, arrays can be transformed using various methods. This article demonstrates how to extract and convert time values from an array of objects into a more usable format. Suppose we have an array of objects containing time strings: const arr = [ {"time":"18:00:00"}, {"time":"10:00:00"}, {"time":"16:30:00"} ]; We need to create a function that: Extracts the time strings from each object Converts times like "18:00:00" to arrays like [18, 0] Returns an array containing all converted time arrays Using map() to Transform ...

Read More

JavaScript Reverse the order of the bits in a given integer

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 329 Views

We are required to write a JavaScript program that reverses the order of the bits in a given integer. For example − 56 -> 111000 after reverse 7 -> 111 Another example, 234 -> 11101010 after reverse 87 -> 1010111 How It Works The algorithm converts the number to binary, reverses the binary string, and converts back to decimal: Convert number to binary string using toString(2) Split into array, reverse, and join back Parse reversed binary string to decimal using parseInt(reversedBinary, 2) Example const ...

Read More

Go through an array and sum only numbers JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 429 Views

We are required to write a JavaScript function that takes in an array. The array might contain any type of value, Number literals, string literals, objects, undefined. Our function should pick all the Number type values and return their sum. Example const arr = [1, 2, 'a', 4]; const countNumbers = (arr = []) => { let sum = 0; for(let i = 0; i < arr.length; i++){ const el = arr[i]; ...

Read More

Finding sum of a range in an array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 365 Views

We are required to write an Array function (functions that lives on Array.prototype object). The function should take in a start index and an end index and it should sum all the elements from start index to end index in the array (including both start and end). Example const arr = [1, 2, 3, 4, 5, 6, 7]; const sumRange = function(start = 0, end = 1){ let sum = 0; if(start > end){ return sum; } for(let i = start; i

Read More

Finding the majority element of an array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 883 Views

We are given an array of size n, and we are required to find the majority element. The majority element is the element that appears more than [ n/2 ] times. Example const arr = [2, 4, 2, 2, 2, 4, 6, 2, 5, 2]; const majorityElement = (arr = []) => { const threshold = Math.floor(arr.length / 2); const map = {}; for (let i = 0; i < arr.length; i++) { const value = arr[i]; ...

Read More

Find the least duplicate items in an array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 470 Views

We are required to write a JavaScript function that takes in an array of literals which may contain some duplicate values. The function should return an array of all those elements that are repeated for the least number of times. For example− If the input array is − const arr = [1, 1, 2, 2, 3, 3, 3]; Then the output should be − const output = [1, 2]; because 1 and 2 are repeated for the least number of times (2) Example const arr = [1, ...

Read More

Check if a value exists in an array and get the next value JavaScript

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

We are required to write a JavaScript function that takes in an array of literals as the first argument and a search string as the second argument. The function should search the array for that search string. If that string exists in the array, we should return its next element from the array, otherwise we should return false. Example const arr = ["", "comp", "myval", "view", "1"]; const getNext = (value, arr) => { const a = [undefined].concat(arr); const p = a.indexOf(value) + 1; ...

Read More

Complicated array grouping JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 233 Views

Suppose we have an array of objects like this − const arr = [ {userId: "3t5bsFB4PJmA3oTnm", from: 1, to: 6}, {userId: "3t5bsFB4PJmA3oTnm", from: 7, to: 15}, {userId: "3t5bsFB4PJmA3oTnm", from: 172, to: 181}, {userId: "3t5bsFB4PJmA3oTnm", from: 182, to: 190} ]; We need to write a JavaScript function that groups consecutive objects based on their "from" and "to" properties. Objects are considered consecutive when the "to" value of one object plus 1 equals the "from" value of the next object with the ...

Read More
Showing 741–750 of 5,340 articles
« Prev 1 73 74 75 76 77 534 Next »
Advertisements