Why does it say arr.push not a function

Tell us what’s happening:
Describe your issue in detail here.
well I was trying to get the array combine all of it numbers so I thought by multiplying it by one it would multiply all the numbers and I get 120 but I got arr.push not a function;

  **Your code so far**

function factorialize(num) {
let arr = [];
 for (let i = num; i > 0; i--) {
  arr.push(i);
  arr *= 1;
 }
 return arr;
}

factorialize(5);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36

Challenge: Factorialize a Number

Link to the challenge:

This wants you to return a number not an array

Ah. So here, this is a bit confusing.

You have an empty array arr. Loop runs once, and pushes 5 into it. So you have [5]. Straight after that, you run arr = [5] * 1. Which is where JS, trying to be helpful, decides what you want to do is arr = 5 * 1. So arr isn’t an array any more, it’s the number 5.

Next time the loop runs, it tries to push 4 into arr. But arr is the number 5, you can’t push to it.

Set arr to a number before the loop, remove the push line

1 Like

What @DanCouper is saying is exactly correct. I would also recommend changing the name of the variable arr to something that would make more sense for a number.

2 Likes

Thanks @DanCouper you are really good at explaining things.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.