Javascript Articles

Page 76 of 534

Determine whether there is a pair of values in the array where the average of the pair equals to the target average in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 179 Views

We are required to write a JavaScript function that takes in an array of sorted integers and a target average as first and second arguments. The function should determine whether there is a pair of values in the array where the average of the pair equals to the target average. There's a solution with O(1) additional space complexity and O(n) time complexity. Since an array is sorted, it makes sense to have two indices: one going from begin to end (say y), another from end to begin of an array (say x). Algorithm Approach The two-pointer ...

Read More

Find the minimum and maximum values that can be calculated by summing exactly four of the five integers in JavaScript

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

Given an array of five positive integers, we are required to find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers. The array is not sorted all the times. For example: const arr = [1, 3, 5, 7, 9] The minimum sum is: 1 + 3 + 5 + 7 = 16 and the maximum sum is: 3 + 5 + 7 ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 488 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 509 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 179 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 274 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 904 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
Showing 751–760 of 5,340 articles
« Prev 1 74 75 76 77 78 534 Next »
Advertisements