https://i.stack.imgur.com/FU2o4.png
I am clearly new at programming. I need a support. I have tried to write a function that contains multiple of 7 that are less than max argument. But after I run my code I saw the result 0 as you can find in the picture. How can I fix it?
function returnSevens(max) {
let sevenarr=[];
for (let i = 0; i<max; i++) {
if (i % 7 === 0) {
sevenarr.push[i];
}
}
return sevenarr;
}
console.log(returnSevens(14))
Your code is correct, you just have a minor typo with your brackets. [] instead of ().
A small hint to optimize the performance of your code:
You can actually skip the whole if condition when increasing your i by 7 instead of 1.
function returnSevens(max) {
let sevenarr=[];
for (let i = 0; i<max; i += 7) {
sevenarr.push(i);
}
return sevenarr;
}
Push is a method. so you need to use () instead of []
function returnSevens(max) {
let sevenarr=[];
for (let i = 0; i<max; i++) {
if (i % 7 === 0) {
sevenarr.push(i);
}
}
return sevenarr;
}
console.log(returnSevens(14))
You already have the answer to your question, but I'd suggest to do some adjustments.
const, not let. So it wont be changed by accident.function returnSevens(max) {
const sevenarr = [];
for (let i = 0; i < max; i+=7) {
sevenarr.push(i);
}
return sevenarr;
}
console.log(returnSevens(14));