Javascript Articles

Page 77 of 534

Generating Random Prime Number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 672 Views

We are required to write a JavaScript function that takes in two numbers specifying a range. Our function should return a random prime number that falls in that range. Algorithm Overview The solution uses the Sieve of Eratosthenes algorithm to find all prime numbers in the range, then randomly selects one. This approach ensures efficiency for finding multiple primes. Example The code for this will be − const range = [100, 1000]; const getPrimes = (min, max) => { const result = Array(max + 1) ...

Read More

How to round up to the nearest N in JavaScript

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

In JavaScript, rounding a number to the nearest multiple of N requires dividing by N, rounding the result, then multiplying back by N. Consider this example: const num = 76; When rounding to different factors: Round to nearest 10: 76 becomes 80 Round to nearest 100: 76 becomes 100 Round to nearest 1000: 76 becomes 0 We need a JavaScript function that takes a number and a rounding factor, returning the rounded result. Syntax const roundOffTo = (num, factor = 1) => { const ...

Read More

Generating a random number that is divisible by n in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 617 Views

We are required to write a JavaScript function that takes in a number as the only argument. The function should then return a random generated number which is always divisible by the number provided by the argument. Approach To generate a random number divisible by n, we generate a random number within a range, divide it by n, round the result, and multiply back by n. This ensures the final number is always a multiple of n. Example The code for this will be: const num = 21; // function that generates random ...

Read More

Flatten array to 1 line in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 283 Views

Suppose, we have a nested array of numbers like this − const arr = [ [ 0, 0, 0, -8.5, 28, 8.5 ], [ 1, 1, -3, 0, 3, 12 ], [ 2, 2, -0.5, 0, 0.5, 5.3 ] ]; We are required to write a JavaScript function that takes in one such nested array of numbers. The function should combine all the numbers in the nested array to form a single string. In the resulting string, the adjacent numbers should be separated by whitespaces and ...

Read More

Map multiple properties in array of objects to the same array JavaScript

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

When working with arrays of objects in JavaScript, you often need to extract multiple property values and combine them into a single flat array. This tutorial shows different approaches to map multiple properties from an array of objects. Problem Statement Given an array of objects like this: const arr = [ {a: 1, b: 2}, {a: 3, b: 4}, {a: 5, b: 6} ]; We need to extract all property values and create a flat array: const output = [1, 2, 3, 4, ...

Read More

Filtering array within a limit JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 782 Views

We are required to write a JavaScript function that takes in an array of numbers as the first argument and an upper limit and lower limit number as second and third argument respectively. Our function should filter the array and return a new array that contains elements between the range specified by the upper and lower limit (including the limits). Syntax const filterByLimits = (arr, upper, lower) => { return arr.filter(element => element >= lower && element { let res = []; res = arr.filter(el => { return el >= lower && el { return arr.filter(element => element >= lower && element = lower && element

Read More

Construct objects from joining two strings JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 362 Views

We are required to write a JavaScript function that takes in two comma separated strings. The first string is the key string and the second string is the value string, the number of elements (commas) in both the strings will always be the same. Our function should construct an object based on the key and value strings and map the corresponding values to the keys. Example const str1= '[atty_hourly_rate], [paralegal_hourly_rate], [advanced_deposit]'; const str2 = '250, 150, 500'; const mapStrings = (str1 = '', str2 = '') => { const keys = ...

Read More

Finding the longest valid parentheses JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 373 Views

Given a string containing just the characters '(' and ')', we find the length of the longest valid (well-formed) parentheses substring. A set of parentheses qualifies to be a well-formed parentheses, if and only if, for each opening parentheses, it contains a closing parentheses. For example: '(())()' is a well-formed parentheses '())' is not a well-formed parentheses '()()()' is a well-formed parentheses Algorithm Approach The solution uses a stack-based approach to track invalid parentheses positions. We push indices of unmatched characters onto the stack, then calculate the longest valid substring between these invalid ...

Read More

Compare two arrays of single characters and return the difference? JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 529 Views

We are required to compare, and get the difference, between two arrays containing single character strings appearing multiple times in each array. Example of two such arrays are: const arr1 = ['A', 'C', 'A', 'D']; const arr2 = ['F', 'A', 'T', 'T']; We will check each character at the same position and return only the parts who are different. Approach The algorithm compares elements at the same index position. When characters differ, both are added to the result array. Any remaining elements from the longer array are also included. Example ...

Read More

Finding trailing zeros of a factorial JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 465 Views

Given an integer n, we have to write a function that returns the number of trailing zeroes in n! (factorial). For example: trailingZeroes(4) = 0 trailingZeroes(5) = 1 because 5! = 120 trailingZeroes(6) = 1 How It Works Trailing zeros are created by multiplying factors of 10, which come from pairs of 2 and 5. Since there are always more factors of 2 than 5 in any factorial, we only need to count factors of 5. We count multiples of 5, then 25 (5²), then 125 (5³), and so on. Example const num = 17; const findTrailingZeroes = num => { let cur = 5, total = 0; while (cur

Read More
Showing 761–770 of 5,340 articles
« Prev 1 75 76 77 78 79 534 Next »
Advertisements