Web Development Articles

Page 174 of 801

Reduce array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 250 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 342 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 436 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 376 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 894 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 480 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 241 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

Determine whether there is a pair of values in the array where the average of the pair equals to the target average in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 187 Views

We are required to write a JavaScript function that takes in an array of sorted integers and a target average as first and second arguments. The function should determine whether there is a pair of values in the array where the average of the pair equals to the target average. There's a solution with O(1) additional space complexity and O(n) time complexity. Since an array is sorted, it makes sense to have two indices: one going from begin to end (say y), another from end to begin of an array (say x). Algorithm Approach The two-pointer ...

Read More

Find the minimum and maximum values that can be calculated by summing exactly four of the five integers in JavaScript

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

Given an array of five positive integers, we are required to find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers. The array is not sorted all the times. For example: const arr = [1, 3, 5, 7, 9] The minimum sum is: 1 + 3 + 5 + 7 = 16 and the maximum sum is: 3 + 5 + 7 ...

Read More
Showing 1731–1740 of 8,010 articles
« Prev 1 172 173 174 175 176 801 Next »
Advertisements