0

Unable to get the factorial output in Java script

function main() { var number = parseInt(readLine(), 10) var factorial = 1; // Your code here var x= number; for(x;x>=1;){ y=x-1} console.log(y*x) }

1st Sep 2021, 2:07 PM
Nishant Kumar
2 Answers
+ 1
Call the function main()
1st Sep 2021, 2:44 PM
Pariket Thakur
Pariket Thakur - avatar
+ 1
Nishant Kumar Here's what mine looks like. It's just one way that works. I added a check for input that was 0 or 1 bc those are always 1. Feel free to message me with questions. I'll help within 24-48 hours bc of school and work. function main() { var number = parseInt(readLine(), 10) var factorial = 1; // Your code here var num = number; if (num == 0 || num == 1) { console.log(1); } else { var i=num-1; for (i; i>1; i--) { num *= i; } console.log(num); } }
1st Sep 2021, 8:21 PM
Tia
Tia - avatar