DEV Community

Lakshya Tyagi
Lakshya Tyagi

Posted on

JavaScript Code Daily Challenge #4

About

This is a series of JavaScript Code Daily Challenge. Each day I show a few solutions written in JavaScript. The questions are from coding practice/contest sites such as HackerRank, LeetCode, Codeforces, Atcoder and etc.

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

'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++];
}
Enter fullscreen mode Exit fullscreen mode

Create function in comment

/*
 * Create the function factorial here
 */
Enter fullscreen mode Exit fullscreen mode
function main() {
    const n = +(readLine());

    console.log(factorial(n));
}
Enter fullscreen mode Exit fullscreen mode

Latest comments (2)

Collapse
 
bugb profile image
bugb • Edited

JS codegolf

factorial=n=>n>1?n*factorial(n-1):1
Enter fullscreen mode Exit fullscreen mode
Collapse
 
lakshyatyagi24 profile image
Lakshya Tyagi
function factorial(n) {
    let fact=1;
    while(n>1)
    {
        fact=fact*n;
        n=n-1;
    }
    return fact;
}
Enter fullscreen mode Exit fullscreen mode