Loop is not incrementing
I am new to javascript.
I have to create a function that returns the next multiples of 5 based on a given number.But my loop repeats the information, it doesn´t increment the result.What am I doing wrong?
function proS(num, mult) {
let arr = []
for (var i = 1; i <= mult; i++) {
arr.push(Math.ceil(num / 5) * 5)
}
return arr
}
proS(6, 4)
returns [10, 10, 10, 10]
Solution 1:
you should use your formula to produce the initial value of num
, then add 5
to it each time through the loop.
function proS(num, mult) {
let arr = []
num = Math.ceil(num / 5) * 5;
for (var i = 1; i <= mult; i++, num += 5) {
arr.push(num)
}
return arr
}
console.log(proS(6, 4))
Solution 2:
I won't write the answer, but I'll point you in the right direction. If you pay close attention arr.push(Math.ceil(num / 5) * 5)
this bit of code is invariant. This means that you should probably add the variable i
somewhere in there so that it actually returns a different output every iteration of the loop.
Solution 3:
thanks guys, I ended up doing this and it worked:
function proS (num, mult){
let arr = []
let res = (Math.ceil(num / 5) * 5)
for (var i = 1; i <= mult; i ++){
arr.push(res)
res += 5
}
return arr
}