Skip to Content

10 days Javascript: Functions

Posted on One min read

Day 1: Functions

Solution for:
https://www.hackerrank.com/challenges/js10-hello-world/problem

Task

Implement a function named factorial that has one parameter: an integer, n. It must return the value of n! (i.e., n factorial).

/*
 * Create the function factorial here
 */
function factorial(n) {
  var sum = 1;
  for (var i = n; i > 1; i--) {
    sum = sum * i;
  }

  return sum;
}

https://www.hackerrank.com/challenges/js10-function/problem