Web Development Articles

Page 175 of 801

How to get the most common values in array: JavaScript ?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 498 Views

We are required to write a JavaScript function that takes in an array of literals that have repeating values. Our function should return an array of the most common element(s) in the array (if two or more elements appear for the same number of most times then the array should contain all those elements). Example The code for this will be − const arr1 = ["a", "c", "a", "b", "d", "e", "f"]; const arr2 = ["a", "c", "a", "c", "d", "e", "f"]; const getMostCommon = arr => { const count = ...

Read More

Complete Equation by Filling Missing Operator in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 521 Views

We are required to write a JavaScript function that takes in a bunch of numbers and returns the correct sequence of operations to satisfy the equation. The operators that can be used are (+, −, *, /, ^, %). For example − Input : 5 3 8 Output : 5+3=8 Input : 9 27 3 Output : 9=27/3 Input : 5 2 25 , 1 5 2 Output : 5^2=25 , 1=5%2 For each input, there is at ...

Read More

How to find a nearest higher number from a specific set of numbers: JavaScript ?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 188 Views

We have a set of numbers and our requirement is to find the same or the nearest higher number key to a specific number provided as the input to the function. The set of numbers is defined as: const numbers = { A: 107, B: 112, C: 117, D: 127, E: 132, F: 140, G: 117, H: 127, I: 132, J: 132, K: 140, ...

Read More

Splitting an object into an array of objects in JavaScript

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

Splitting an object into an array of separate objects is useful when you need to transform key-value pairs into individual objects for easier manipulation or iteration. Suppose we have an object like this: const obj = { "name": "John", "age": 30, "city": "New York", "profession": "Developer" }; We need to write a JavaScript function that takes such an object and returns a new array where each key-value pair becomes its own separate object. Using Object.keys() and forEach() const obj = ...

Read More

Evaluating a string as a mathematical expression in JavaScript

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

We are required to write a JavaScript function that takes in a stringified mathematical equation. The function should return the result of the equation provided to the function. For example: If the equation is − const str = '1+23+4+5-30'; Then the output should be 3 Example The code for this will be − const str = '1+23+4+5-30'; const compute = (str = '') => { let total = 0; str = str.match(/[+\-]*(\.\d+|\d+(\.\d+)?)/g) || []; while (str.length) { ...

Read More

JavaScript: Combine highest key values of multiple arrays into a single array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 279 Views

We are required to write a JavaScript function that takes in any number of arrays of numbers. Our function should return an array of greatest numbers picked from the corresponding positions of the input arrays. The number of elements in the output array should be equal to the length of the longest input array. Problem Understanding Given multiple arrays, we need to compare elements at the same index positions and select the maximum value from each position to form a new array. Example The code for this will be: const arr1 = [117, 121, ...

Read More

Number to alphabets in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 916 Views

We are required to write a JavaScript function that takes in a string of any variable length that represents a number. Our function is supposed to convert the number string to the corresponding letter string. For example − If the number string is − const str = '78956'; Then the output should be − const output = 'ghief'; If the number string is − const str = '12345'; Then the output string should be − const output = 'lcde'; Notice how we ...

Read More

Multiply and Sum Two Arrays in JavaScript

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

We are required to write a JavaScript function that takes in two arrays of equal length. The function should multiply the corresponding (by index) values in each, and sum the results. For example: If the input arrays are − const arr1 = [2, 3, 4, 5]; const arr2 = [4, 3, 3, 1]; then the output should be 34, because: (2*4 + 3*3 + 4*3 + 5*1) = 8 + 9 + 12 + 5 = 34 Using for Loop The most straightforward approach uses a for loop to iterate ...

Read More

Generating Random Prime Number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 680 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
Showing 1741–1750 of 8,010 articles
« Prev 1 173 174 175 176 177 801 Next »
Advertisements