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

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

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