Javascript Articles

Page 93 of 534

Finding two numbers given their sum and Highest Common Factor using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 273 Views

We are required to write a JavaScript function that takes in two numbers. The first number represents the sum of two numbers and second represents their HCF (GCD or Greatest Common Divisor). Our function should find and return those two numbers. Problem Given the sum and HCF of two numbers, we need to find the original two numbers. For two numbers to have a specific HCF, both numbers must be multiples of that HCF. Mathematical Approach If two numbers a and b have HCF h, then: a = h × m and b = ...

Read More

Returning an array containing last n even numbers from input array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 222 Views

Problem We are required to write a JavaScript function that takes in an array of numbers as the first argument and a number as the second argument. Our function should pick and return an array of last n even numbers present in the input array. Example Following is the code − const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const num = 3; const pickEvens = (arr = [], num = 1) => { const res = []; for(let index = ...

Read More

Constructing an array of first n multiples of an input number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 415 Views

We are required to write a JavaScript function that takes in two numbers, let say m and n. Our function should construct and return an array of first n natural multiples of m. Problem Statement Given two numbers m and n, we need to create a function that returns an array containing the first n multiples of m. For example, if m = 6 and n = 5, the function should return [6, 12, 18, 24, 30]. Method 1: Using a For Loop The most straightforward approach is to use a for loop to iterate ...

Read More

Breaking a string into chunks of defined length and removing spaces using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 317 Views

We are required to write a JavaScript function that takes in a string sentence that might contain spaces as the first argument and a number as the second argument. Our function should first remove all the spaces from the string and then break the string into a number of chunks specified by the second argument. All the string chunks should have the same length with an exception of last chunk, which might, in some cases, have a different length. Solution Approach The solution involves two main steps: Remove all spaces using ...

Read More

Finding the only out of sequence number from an array using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 506 Views

Problem We are required to write a JavaScript function that takes in an array of numbers. The array is sorted in ascending order and only one element in the array is out of order. Our function should find and return that element. Approach The solution works by checking each element against its neighbors. When we find an element that is greater than the next element AND the next element is also greater than the element after it, we've found our out-of-sequence number. Example Following is the code: const arr = [1, 2, 3, 4, 17, 5, ...

Read More

Finding the sum of minimum value in each row of a 2-D array using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 362 Views

Problem We are required to write a JavaScript function that takes in a 2-D array of numbers. Our function should pick the smallest number from each row of the 2-D array and then finally return the sum of those smallest numbers. Example Following is the code − const arr = [ [2, 5, 1, 6], [6, 8, 5, 8], [3, 6, 7, 5], [9, 11, 13, 12] ]; const sumSmallest = (arr = []) => { const findSmallest = array => ...

Read More

Greatest number divisible by n within a bound in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 413 Views

Problem We need to write a JavaScript function that takes in a divisor number n and a bound number b. Our function should find the largest integer num, such that: num is divisible by n (the divisor) num is less than or equal to b (the bound) num is greater than 0 Brute Force Approach The straightforward approach is to iterate through all multiples of n and find the largest one within the bound: const n = 14; const b = 400; const biggestDivisible = ...

Read More

Finding the sum of all common elements within arrays using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 336 Views

Problem We are required to write a JavaScript function that takes in three arrays of numbers. Our function should return the sum of all those numbers that are common in all three arrays. Example Following is the code − const arr1 = [4, 4, 5, 8, 3]; const arr2 = [7, 3, 7, 4, 1]; const arr3 = [11, 0, 7, 3, 4]; const sumCommon = (arr1 = [], arr2 = [], arr3 = []) => { let sum = 0; for(let i = 0; i ...

Read More

Finding the nth power of array element present at nth index using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 377 Views

We need to write a JavaScript function that takes an array of numbers and maps each element to its power based on its 0-based index position. For example, the element at index 0 is raised to power 0, the element at index 1 is raised to power 1, and so on. Problem Statement Create a function that transforms an input array where each element is raised to the power of its index position, then return the resulting array. Example Here's how to implement this using a traditional for loop: const arr = [5, 2, ...

Read More

Summing cubes of natural numbers within a range in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 212 Views

Problem We are required to write a JavaScript function that takes in a range array of two numbers. Our function should find the sum of all the cubes of the numbers that falls in the specified range. Understanding the Problem Given a range [a, b], we need to calculate a³ + (a+1)³ + (a+2)³ + ... + b³. For example, with range [4, 11], we calculate 4³ + 5³ + 6³ + 7³ + 8³ + 9³ + 10³ + 11³. Solution Using For Loop Here's the implementation that iterates through the range and sums the cubes: const range = [4, 11]; const sumCubes = ([l, h]) => { const findCube = num => num * num * num; let sum = 0; for(let i = l; i

Read More
Showing 921–930 of 5,340 articles
« Prev 1 91 92 93 94 95 534 Next »
Advertisements