function || Js Hackerrank Solution

 Task

Implement a function named factorial that has one parameter: an integer, . It must return the value of

(i.e., factorial).




Input Format

Locked stub code in the editor reads a single integer, , from stdin and passes it to a function named

factorial.

Constraints

Output Format

The function must return the value of .

Sample Input 0

4

Sample Output 0

24

 

Solution:

'use strict';

 

  process.stdin.resume();

  process.stdin.setEncoding('utf-8');

 

  let inputString = '';

  let currentLine = 0;

 

  process.stdin.on('data', inputStdin => {

    inputString += inputStdin;

  });

 

   Process.stdin.on('end', _ => {

    inputString = inputString.trim().split('\n').map(string => {

        return string.trim();

    });

   

    main();   

  });

 

  function readLine() {

    return inputString[currentLine++];

  }

 

  function factorial(n){

    if(n==0){

        return 1;

    }else{

        return (n*factorial(n-1));

    }

   

  }

 

  function main() {

    const n = +(readLine());

   

    console.log(factorial(n));

  }



No comments:

Post a Comment