Coding challange: factoralize number

Hello all,

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?

THank you

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

Thank you very much, I was missing the ‘let’ before the loop starting point.
Since it ran normally on repl.it i didnt notice it :slight_smile:

EDIT:

Nvm, its still working on repl.i in Strict mode, but fcc doesnt recognise it as a valid answer

what does this return?
console.log(factorialize(5))

ReferenceError: factorialize is not defined
@/script.js:12:1

at /script.js:12:1

my error, your function is named factoralize
just wrap in a console
log your function call

Sorry… My Typo.

'use strict'

function factoralize (num)
{
  let factNum = 1;
  for (let i = 1; i<= num; i++)
  {
    factNum *= i;
  }
  console.log(factNum);
}
console.log(factoralize(5));

logs:
undefined

120
undefined

to see it better
console.log("My function returns " + factoralize(5))

can you think why this is what your function returns?

This is what i am getting… i dont know why fcc doesnt recognise it, since the fucntion returns what is expected

I told you to use the function call
console.log("My function returns " + factoralize(5))

the function call is the only way to log the returned value, and that is the value that matters.

do this, and see what it returns.

I get this error:

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.

With the changes, my code stopped working haha

EDIT: I added a return numFact after the loop and it started printing again

this gets me sometimes because when I add a console.print() to the end of my function, it logs the response correctly even without the return.

Anyway, the Fcc still doesnt recognize my code as a possible answer to the question :confused:

Sorry Randell,

I was sending SS as a way to show the code and the console at the same time. My current version is the following:

function factorialize (num)
{
  let factNum = 1;
  for (let i = 1; i<= num; i++)
  {
    factNum *= i;
  }
  return factNum;
}
console.log("My function returns " + factorialize(num));

what’s the challenge link?

anyway, I am pretty sure you are getting error num is not defined

The link is https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/factorialize-a-number

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"

two things: whole code you have in the editor, and errors you get opening the browser console

what do you mean? Do you want me to copy those things?