I am trying to make this code work but i am having some problems. I should create a function to factoralize any natural numbers.
My code is the following:
function factoralize (num)
{
let factNum = 1;
for (i = 1; i<= num; i++)
{
factNum *= i;
}
console.log(factNum);
}
factoralize (5);
I have tested it using Repl.it with mulpile examples and they all return the expected results. Freecodecamp however doesnt recognize it as a valid answer… Can you help me understand what is going on?
you need to declare/initialise any variable you use
are all your variables defined with var/const/let?
if you want to see the error on repl.it write 'use strict' at the top of the editor, single quotes included. All the code on fcc is executed in strict mode
you should have not called the function inside itself. That’s recursion, it is introduced later.
you should substitute the function call that you already have, the last line of code, with that line. So you see what it returns and it is clearly identified between the many results in the console
function factoralize(num) {...}
console.log("My function returns " + factoralize(4));
with this you should clearly see what it returns. Try thinking of why it returns that.
on the challenge link I am getting all the tests wrong. In repl.it all the tests log the correct number.
the FCC console shows the following after i run the code:
// running tests
factorialize(5) should return a number.
factorialize(5) should return 120.
factorialize(10) should return 3628800.
factorialize(20) should return 2432902008176640000.
factorialize(0) should return 1.
// tests completed
// console output
"My function returns 120"
"My function returns 120"
"My function returns 120"
"My function returns 120"
"My function returns 120"