Tell us what’s happening:
How could this being scripted into a loop? (alternative which is supposed to be faster)
Your code so far
let factorialize = (num) => {
return num == 0 ? 1 : num * factorialize(num-1)
}
factorialize(5);
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36 OPR/62.0.3331.119.
Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.
function factorialize(num) {
let x = 1
for (let i = 0; i < num; i++) {
x *= x + 1
}
return x;
}
console.log(factorialize(0))// works just with 0
Second try:
function factorialize(num) {
for (let i = num; i > 0; i--) {
num *= num
}
return num;
}
console.log(factorialize(5)// doesn't factorize anything
This --for me-- was the less far from any solution:
function factorialize(num) {
let x;
for (let i = num; i > 0; i--) {
x = num * (num-1)
}
return x;
}
console.log(factorialize(5))// prompts 20 I think because It just iterates one time, but I couldn't understand why.
in none if your loops you use the changing variable - when you do recursion you get num * num-1 * ... * 2 * 1 because the argument of the function changes, but now you do not have any number that changes by one that you use. you have i but you are not using it