Push numbers in an array using for loop

How can I push numbers using for loops

function factorialize(num) {
  let arr = [];
for (let i = 0;i <= num ; i++){
 return arr.push[i];

}

}

console.log(factorialize(5));

arr should be

[0,1,2,3,4,5]
1 Like

return can only happen once. As soon as a return statement is reached, the function ends.

your return needs to be outside the for loop. Once the loop goes through once it hits return and exits. Leaving return out of the for loop lets the loop go all the way through.

function factorialize(num) {
  let arr = [];
for (let i = 0;i <= num ; i++){
arr.push[i]

}
return arr;
}

The arr.push needs parenthesis() instead of the square brackets .

1 Like