I cant make Factorial task | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I cant make Factorial task

Factorial of number 5 is (5*4*3*2*1) understand? So, i have my premium task, to make factorial by the function “for” with loops. I cant understand how to make it right because every try is all wrong or some tests are right but not all.

12th Oct 2020, 7:46 PM
Alex Lovser
Alex Lovser - avatar
7 Answers
+ 11
Alex , you are right saying that factorial of 5 is 5*4*3*2*1. - to calculate it, you need a for loop. i think you are familiar with that. - so use the for loop combined with a range object that contains all numbers you need (1...5) or (2...5 - as multiplying with 1 does not change anything). - so you just have to make a variable that holds the current product, and update it in each loop iteration. that's all.
12th Oct 2020, 7:55 PM
Lothar
Lothar - avatar
+ 10
Alex as Denise Roßberg suggested 👍 If you need help, you can post the code you're struggling with. Try to search for similar questions or answers before posting.  • https://www.sololearn.com/post/75089/?ref=app
12th Oct 2020, 7:51 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 10
Alex I hope that based on these examples you will get an idea and succeed in solving your task.👍 • https://code.sololearn.com/WMt3NCSb4mrO/?ref=app
12th Oct 2020, 9:12 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 9
Hello Alex can you please share your code so that we can see what went wrong?
12th Oct 2020, 7:47 PM
Denise Roßberg
Denise Roßberg - avatar
+ 7
You need to store the value of factorial. Here are some possibilities how to solve this task: https://www.google.com/amp/s/www.freecodecamp.org/news/how-to-factorialize-a-number-in-javascript-9263c89a4b38/amp/
12th Oct 2020, 8:07 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
process.stdin.setEncoding('utf-8'); let inputString = ''; process.stdin.on('data', inputStdin => { inputString += inputStdin; }); process.stdin.on('end', _ => { main(inputString); }); function main(input) { var number = parseInt(input, 10) var factorial = number - 1; for (idk what to write) console.log(number * factorial--) } Denise Roßberg
12th Oct 2020, 7:50 PM
Alex Lovser
Alex Lovser - avatar
0
Lothar in case all is simple i used to use “for” for looping but i cant make the correct loop
12th Oct 2020, 8:00 PM
Alex Lovser
Alex Lovser - avatar