Web Development Articles

Page 178 of 801

Counting number of vowels in a string with JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 760 Views

We are required to write a JavaScript function that takes in a string. The function should count the number of vowels present in the string. The function should prepare an object mapping the count of each vowel against them. Example The code for this will be − const str = 'this is an example string'; const vowelCount = (str = '') => { const splitString = str.split(''); const obj = {}; const vowels = "aeiou"; splitString.forEach((letter) => { ...

Read More

Sum identical elements within one array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 337 Views

We are required to write a JavaScript function that takes in an array of numbers. The array might contain some repeating / duplicate entries within it. Our function should add all the duplicate entries and return the new array thus formed. Example The code for this will be − const arr = [20, 20, 20, 10, 10, 5, 1]; const sumIdentical = (arr = []) => { let map = {}; for (let i = 0; i < arr.length; i++) { ...

Read More

Return the greatest possible product of n numbers from the array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 164 Views

We are required to write a JavaScript function that takes in an array of Numbers as the first argument and a number, say n, as the second argument. Our function should calculate and return the greatest possible product of n numbers from the array. Problem Analysis To find the maximum product of n numbers, we need to consider both positive and negative numbers. Two negative numbers multiplied together give a positive result, so the strategy involves: Sorting the array to identify the largest and smallest values Choosing ...

Read More

Returning the highest value from an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 320 Views

We are required to write a JavaScript function that takes in an array of Numbers. Our function should iterate through the array and pick the greatest (largest) element from the array and return that element. Using a Custom Loop Function Here's a manual approach that iterates through the array to find the maximum value: const arr = [5, 3, 20, 15, 7]; const findGreatest = (arr = []) => { let greatest = -Infinity; if (!arr?.length) { return null; ...

Read More

Find first duplicate item in array in linear time JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 352 Views

We are required to write a JavaScript function that takes in a read only array of n + 1 integers between 1 and n. The function should find one number that repeats in linear time and using at most O(n) space. For example If the input array is − const arr = [3, 4, 1, 4, 1]; Then the output should be − 4 If there are multiple possible answers (like above), we should output any one. If there is no duplicate, we should output -1. Using Set Data ...

Read More

Finding all possible combined (plus and minus) sums of n arguments JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 390 Views

We need to write a JavaScript function that takes any number of arguments (all numbers) and finds the sum closest to zero from all possible combinations of addition and subtraction. For example, with arguments 1, 2, 3, the possible combinations are: 1 + 2 + 3 = 6 1 - 2 - 3 = -4 1 + 2 - 3 = 0 1 - 2 + 3 = 2 The function should return the sum closest to 0, which in this case is 0. Algorithm Approach We use a Set to store all ...

Read More

Number of letters in the counting JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 433 Views

We are required to write a JavaScript function that takes in a number, say n. The function should count the letters in the number names from 1 to n. For example − If n = 5; Then the numbers are one, two, three, four, five. And the total number of letters are 19, so the output should be 19. How It Works The algorithm uses arrays to store the letter count for each digit position and handles special cases like teens (11-19) and compound numbers (hundred, thousand). Example const sumUpto = (num = ...

Read More

Sorting numbers in descending order but with `0`s at the start JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 544 Views

We are required to write a JavaScript function that takes in an array of numbers. The function should sort the array of numbers on the following criteria: If the array contains any zeros, they should all appear in the beginning. All the remaining numbers should be placed in a decreasing order. For example, if the input array is: const arr = [4, 7, 0, 3, 5, 1, 0]; Then after applying the sort, the array should become: const output = [0, 0, 7, 5, ...

Read More

Can form target array from source array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 244 Views

We are given an array of distinct integers, let's say arr, and another array of integer arrays, let say sourceArr. In the sourceArr array, the integers are distinct. We should write a function that forms arr by concatenating the arrays in sourceArr in any order. However, we cannot reorder the integers inside of any subarray in the sourceArr. We should return true if it is possible to form the array arr from sourceArr, false otherwise. For example: const arr = [23, 67, 789]; const sourceArr = [[23], [789, 67]]; The function should return ...

Read More

Sum of all multiples in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 346 Views

We need to write a JavaScript function that takes a number n as the first argument, followed by any number of divisor arguments. The function should sum all numbers up to n that are divisible by any of the specified divisors. Problem Statement Given a number n and multiple divisors, find the sum of all numbers from 1 to n that are multiples of any of the divisors. For example, if we call: sumMultiples(15, 2, 3) We need to find numbers up to 15 that are divisible by either 2 or 3: ...

Read More
Showing 1771–1780 of 8,010 articles
« Prev 1 176 177 178 179 180 801 Next »
Advertisements