Basic Algorithm Scripting - Factorialize a Number

Tell us what’s happening:

When I run the following code. None of the tests pass. Does anyone know why?

The console.log() at the bottom of the code is showing that the function is returning the correct number.

Is there something I’m not understanding?

Thank you!

Your code so far

let arr = []
let number = 1
function factorialize(num) {
  for (let i = num ; i > 1; i--){
  for (let i = 0; i < arr.length; i++){
    number = number * arr[i]
  return number


Your browser information:

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

Challenge: Basic Algorithm Scripting - Factorialize a Number

Link to the challenge:

Try adding console.log(arr) after the for loop that pushes all of the numbers to arr.

global / local variable issue.

1 Like

Hey there! So the issue with your program is that you’re declaring the variables arr and number outside your function, making them Global variables. Now this still technically works for the purposes of your program but it isn’t what is desired of the test cases. You generally want to only use local variables or variables delcared inside your function to perform calculations and changes to your input. Regardless, in order to pass the test you’d need to delcare your variables locally instead of globally as well as add a condition incase the user passes a number <= 0. Hope this helps and happy coding!

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