Javascript Function Creation Question 12

Introduction

Write a function factorial() that, given a number n, returns the product (multiplication) of all positive integers from 1 up to n (inclusive).

For example, factorial(3) = 123; factorial(5) = 12345.


function factorial(num) {
  //your code
}
var factorial_result = factorial(5);
console.log(factorial_result);




function factorial(num) {
  if (num < 0) {
    return "The given argument must be positive integer.";
  }
  var fact = 1;
  for (var i = 1; i <= num; i++) {
    fact *= i;
  }
  return fact;
}
var factorial_result = factorial(5);
console.log(factorial_result);



PreviousNext

Related