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) {
arr.push(num)
for (let i = num ; i > 1; i--){
num--
arr.push(num)
}
for (let i = 0; i < arr.length; i++){
number = number * arr[i]
}
return number
}
console.log(factorialize(5))
console.log(number)
console.log(arr)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36
Challenge: Basic Algorithm Scripting - Factorialize a Number
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!