loading...

JS FUNCTIONS AND MORE BASIC QUESTIONS

bushboy profile image Bush-boy ・1 min read

Am learning how to create webpages, i have gone through basic html n css, now am trying to make javascript work but theres alot am not understanding about the code, so i was just viewing fuctions in js and having trouble understanding this statement...

const factorial = function fac(n) { return n < 2 ? 1 : n * fac(n - 1) }

console.log(factorial(3))

Discussion

pic
Editor guide
Collapse
ignasave profile image
Ignacio Martin Vazquez

Is a recursive function, it says
Function factorial recieves a number.
Then, if the number is less than 2, returns 1, else return the number plus the result of the same function, but passing the number -1. Si if You call factorial of 4, ir would compare if it's less than 2, as isnt, it would return the multiplication of 4 by the result of factorial of 3, then it would compare if 3 is less than 2, it is not so, se have the same thing, it's gonna wait the result of 3 by factorial of 2, 2 is not less than 2, so we get in the implicit end of the recursive call, 2 * factorial of 1 , here, 1 is less than 2, so ir returns 1, and all the return begins to work. If You need more explination search for recursive functions

Collapse
bushboy profile image
Bush-boy Author

thanks for the explanation, great im on it.... am learning js btw i want to get into webdev stuff... any advice here???

Collapse
ignasave profile image
Ignacio Martin Vazquez

Don't worry, yes seach in YouTube for web dev roadmaps, that Will hice You a Nice overview of what You can learn and in wich order You need yo do so, there is a Lot of websites and apps yo learn each thing, but most importante make projects nonstop

Thread Thread
bushboy profile image
Bush-boy Author

Thanks, i really appreciate, have a good day, que tengas un buen día!!!