https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Return_values

Hi team,
I was trying to solve factorial number of my own logic and I get some weird result, below is my code please help me with this

function Fact(n){
var m = n;
//console.log(m) // 5 initially
if(m === 1){return 1}
for(var i=m; i>=1; i–){
m*=i-1;
//console.log(m); // I get 120 here
}
return m; // this return statement is always 0, I’m not getting 120
}
Fact(5);

Hello @socalledsoftwaredeveloper

you doing great well done just some little issue

  1. the main issue is here
for(var i=m; i>=1; i–){  // i guess there typo here it should be i-- not i-
m*=i-1;              // when i be 1 then m will be m*=i-1 (which will be 0) which make m to be 0
  1. there a missing semicolon at the end of return 1;

  2. notice that your email is visible so try to change your profile setting to hide it

  3. it would be better to share your code on any online service like codepen.io jsfiddle github glitch in your future post

hope that help and have a nice day :slight_smile:

2 Likes

Thank you Team, sure next time I’ll follow the pattern

1 Like

you very welcome @socalledsoftwaredeveloper

i am not the team i am just a learner same as you :wink:

i forget to mention that it better to use let instead of var cause it give you a block level scope

2 Likes

I appreciate very much @justsomeone, thank you for your additional tips too.

1 Like

you very welcome and happy coding :slight_smile:

Yes, you are THE team :sweat_smile: :sweat_smile:

1 Like

is that a job offer boss :joy:

1 Like

Yes! You can do as much work as you like :grin:

1 Like

loool did not expect that well done boss

1 Like