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) }
2 Respostas
+ 1
Call the function
main()
+ 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);
}
}