Practice with arrays

Hello, I’ve just started the intermediate practice on arrays, loops, objects ad iterators. Feel pretty good that I managed to work this one out. It returns the correct answer but a) when I ask codecademy to check my answer it states that this doesn’t work if I input the number 1 as an argument in the function.

Error: Tested factorial(1) and it did not return 1 .

It does work when I run it though.

const factorial = num => {

  let array = []

  while (num > 0) {

    array.push(num);

    num--;
  } console.log(array);
  console.log(array.reduce((a,b) => a*b));
}
factorial(6);

The phrasing of the error being

implies to me that the exercise wanted you to return the values. However currently the function returns undefined, as you are using console.log() inside the function to print out the value, instead of actually returning the value. Try changing

to return and see if that makes a difference.

Ah yes, I was initially going to ask about the undefined but I deleted one of the console logs while I was writing the post and it was no longer a problem.
It only requires one value as the answer - it should a product of the numbers counting down from 6 or whatever is input with the function call.

I believe your calculation and actual function is correct, I think it just needs to be returned from the function instead of logged to the console, so hopefully changing that over would fix it.

1 Like

You are right, that fixed it. Thank you! I still don’t understand why it complained though.

1 Like