Basic Algorithm Scripting - Factorialize a Number

Tell us what’s happening:
Describe your issue in detail here.
Why is my solution not working here? I am doing the product 54321 so it returns 120. Is it because this would not work if function would be called with different value of num ?
Your code so far

function factorialize(num) {
let product = 1;
if(num == 1) {
  return 0;
}
product *= num;
console.log(product);
product *= num-1;
console.log(product);
product *= num-2;
console.log(product);
product *= num-3;
console.log(product);
product *= num-4;
console.log(product);
return product;
}

factorialize(5);

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

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/factorialize-a-number`Preformatted text`

Explain to us in words (not code) how your solution returns the correct answer, for example, if I called it as:

factorialize(10);

It wouldn’t. I tried a different solution but still it does not work

function factorialize(num) {
  let product = 1;
  for(let i= 2; i<= 5; i++) {
product *= i;
console.log(product);
  }
  return product;
}

factorialize(5);

product * i so it goes like this:

1 * 2
2 * 3
3 * 4
4 * 5

And yet it fails…

Still, this is important. Can you describe in words and zero code how this process should work?

If we are passing in a number through the num parameter then it seems like that might be important information. Do you agree?

Yes. I tried one more solution, using num this time

function factorialize(num) {

  let factorialNumber = num * (num-1);
  console.log(factorialNumber);
  let factorialNumber2 = factorialNumber * (num-2);
  console.log(factorialNumber2);
  let factorialNumber3 = factorialNumber2 * (num-3);
  console.log(factorialNumber3);
  let factorialNumber4 = factorialNumber3 * (num-4);
  console.log(factorialNumber4);

  return factorialNumber4;
}

factorialize(5);

Description without code:

I am trying to build a variable that stores the value of 120, obtained by multiplying the value passed (num) * his precedessor (num-1), then num-2 and so on. Eventually, I try returning the value of my last variable (120) and yet it fails.

I checked the solution already, and yes it makes sense but I still wanted to come up with a new solution by myself. I think my last solution should’ve worked though

This is your first issue. That’s not the objective of this function. This is the objective:

“Return the factorial of the provided integer. If the integer is represented with the letter n, a factorial is the product of all positive integers less than or equal to n.”

I do not see the number 120 in that objective.

“For example: 5! = 1 * 2 * 3 * 4 * 5 = 120

This is just an example. They could have used the following example instead:

“For example: 4! = 1 * 2 * 3 * 4 = 24

And this would not change how your function should be implemented.

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