DEV Community

Lakshya Tyagi
Lakshya Tyagi

Posted on

2 1

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

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top 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

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay