Skip to content
Advertisement

Tag: algorithm

Convert JavaScript Array [a, b, c, d, e] into [a+b, b+c, c+d, d+e] dynamically

I have an array [a, b, c, d, e, …..] . How can convert this like [a+b, b+c, c+d, d+e, ….] The reducer may be the best way to do this but how to store temp memory after calculation. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce Please check this code Answer There are some issues in your code: The randomIntegerArray function can only deal with lengths

javascript: return every possible pairs in array

the title explains it all somehow I’d like to use the method “Combination” that math has, this is the Wikipedia page to be clear: https://en.wikipedia.org/wiki/Combination I have already found the solution with two loops, I want to do it in one loop example: Answer You can use Array.flatMap() to iterate the array, and Array.map() to iterate all items after the

JavaScript create two dimensional array

I’m new to JavaScript, I’m trying to solve leetcode question 37. I need to a create a blank two dimensional array, I initially used the method in the comments; however, it doesn’t work correctly, it will change all the value. Then, I used the for loop method to create array and currently it worked correctly. But I still cannot figured

Round Robin algorithm with people added and deleted

Ok, in this codepen I already found a Round Robin tournament scheduling algorithm: https://codepen.io/Piconey/pen/mwPamw I use it for a speeddating even where you can date with everyone. My problem: After every round new people can join the event or leave the event (if they get bored 😉 Note: The latecomers don’t need to date everyone, because they already missed x

happy number algo question solution not working

trying to figure out this coding problem: Write an algorithm to determine if a number n is “happy”. A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or

Output Even Numbers In an Array

I want to write a program that receives a list of numbers, converts them into an array, and output the even numbers in the array with Modulus Operator and For Loop. The code does not run as expected: Thank you Answer It’s a matter of incorrect condition. i % 2 would give 0 for even numbers

Print number patterns in JavaScript

I want to print numbers in pattern as below also I need this to print using only one for loop not in if condition inside for loop. If I give s = 7 the output pattern would be 7, 5, 3, 1, 3, 5, 7 If s=6 then output is 6, 4, 2, 4, 6 This is what I tried

Advertisement