javascript factorial
function factorial(n) {
if (n < 0) return;
if (n < 2) return 1;
return n * factorial(n - 1);
}
javascript factorial
function factorial(n) {
if (n < 0) return;
if (n < 2) return 1;
return n * factorial(n - 1);
}
factorial javascript
// Simple arrow function solution
var factorial = n => (n <= 0) ? 1: n * factorial(n - 1);
factorial in javascript
// program to find the factorial of a number
// take input from the user
const number = parseInt(prompt('Enter a positive integer: '));
// checking if number is negative
if (number < 0) {
console.log('Error! Factorial for negative number does not exist.');
}
// if number is 0
else if (number === 0) {
console.log(`The factorial of ${number} is 1.`);
}
// if number is positive
else {
let fact = 1;
for (i = 1; i <= number; i++) {
fact *= i;
}
console.log(`The factorial of ${number} is ${fact}.`);
}
//output
Enter a positive integer: 5
The factorial of 5 is 120.
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us