Web Development Articles

Page 415 of 801

Sum is to be calculated for the numbers in between the array's max and min value JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 213 Views

We need to write a function called sumBetween() that takes an array of two elements and returns the sum of all integers between those values, including both endpoints. For example: [4, 7] = 4+5+6+7 = 22 [10, 6] = 10+9+8+7+6 = 40 Understanding the Problem The function should work regardless of which number is larger. Whether we pass [4, 7] or [7, 4], we need to sum all integers from 4 to 7 inclusive. Solution Using Mathematical Formula We can use the mathematical formula for sum of consecutive integers: sum from 1 ...

Read More

Get the item that appears the most times in an array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 429 Views

Let's say we are required to write a function that takes in an array of string/number literals and returns the index of the item that appears for the most number of times. We will iterate over the array and prepare a frequency map, and from that map we will return the index that makes the most appearances. Example const arr1 = [12, 5, 6, 76, 23, 12, 34, 5, 23, 34, 65, 34, 22, 67, 34]; const arr2 = [12, 5, 6, 76, 23, 12, 34, 5, 23, 34]; const mostAppearances = (arr) => { ...

Read More

How to slice an array with wrapping in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 412 Views

Let's say, we are required to write an array method that overwrites the default Array.prototype.slice(). Usually the Array.prototype.slice() method takes in two arguments the start index and the end index, and returns a subarray of the original array from index start to end-1. What we wish to do is make this slice() function so it returns a subarray from index start to end and not end-1. We iterate over the array using a for loop which is faster than many array methods. Then return the required subarray, lastly we overwrite the Array.prototype.slice() with the method we just wrote. ...

Read More

How to reduce an array while merging one of its field as well in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 546 Views

Consider, we have the following array of objects − const arr = [{ id: 121, hobby: 'cycling' }, { id: 125, hobby: 'jogging' }, { id: 129, hobby: 'reading' }, { id: 121, hobby: 'writing' }, { id: 121, hobby: 'playing football' }, { id: 125, hobby: 'cooking' }, { id: 129, hobby: 'horse riding' }]; ...

Read More

How to make a function that returns the factorial of each integer in an array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 756 Views

We are here required to write a JavaScript function that takes in an array of numbers and returns another array with the factorial of corresponding elements of the array. We will first write a recursive method that takes in a number and returns its factorial and then we will iterate over the array, calculating the factorial of each element of array and then finally we will return the new array of factorials. Therefore, let's write the code for this Creating the Factorial Function First, we'll create a recursive function to calculate the factorial of a single number: ...

Read More

Find n highest values in an object JavaScript

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

Let's say, we have an object that describes various qualities of a football player like this − const qualities = { defence: 82, attack: 92, heading: 91, pace: 96, dribbling: 88, tenacity: 97, vision: 91, passing: 95, shooting: 90 }; We wish to write a function that takes in such object and a number n (n ≤ no. of keys ...

Read More

How to separate alphabets and numbers from an array using JavaScript

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

We have an array that contains some number literals and some string literals mixed, and we are required to write a sorting function that separates the two and the two types should also be sorted within. This approach uses a custom comparator function with JavaScript's sort() method to first separate numbers from strings, then sort each type individually. Using Custom Sort Comparator The code for this sorting function will be: const arr = [1, 5, 'fd', 6, 'as', 'a', 'cx', 43, 's', 51, 7]; const sorter = (a, b) => { ...

Read More

Positive integer square array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 385 Views

In JavaScript, we often need to filter arrays to extract specific types of numbers and perform operations on them. This example demonstrates how to find positive integers in an array and return the sum of their squares. Problem Statement Given an array containing various numbers (positive, negative, decimals, and integers), we need to write a function that returns the sum of squares of all positive integers only. Solution We'll use the reduce() method to iterate through the array and accumulate the sum of squares for positive integers: const arr = [1, -4, 6.1, 0.1, ...

Read More

Make numbers in array relative to 0 – 100 in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 301 Views

Let's say, we have an array that contains some numbers, our job is to write a function that takes in the array and maps all the values relative to 0 to 100. This means that the greatest number should get replaced by 100, the smallest by 0, and all others should get converted to specific numbers between 0 and 100 according to the ratio. Understanding the Formula To normalize values to a 0-100 scale, we use the formula: normalizedValue = ((value - min) / (max - min)) * 100 This formula ensures the smallest ...

Read More

Inverse operation in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 993 Views

The inverse operation on a binary string involves flipping each bit: converting all 0s to 1s and all 1s to 0s. This is a common operation in computer science and digital logic. Understanding Binary Inverse Binary inverse (also called bitwise NOT or complement) transforms each digit in a binary string to its opposite value. For example, '1101' becomes '0010'. Method 1: Using Array Methods We can split the string into individual characters, transform each one, and join them back: const num = '1101'; const n = '11010111'; const inverseBinary = (binary) => { ...

Read More
Showing 4141–4150 of 8,010 articles
« Prev 1 413 414 415 416 417 801 Next »
Advertisements