DEV Community

Lakshya Tyagi
Lakshya Tyagi

Posted on • Updated on

JavaScript Code Daily Challenge #2

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.

Data Types - #2
https://www.hackerrank.com/challenges/js10-data-types/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

Complete the function in comment

function performOperation(secondInteger, secondDecimal, secondString) {
    // Declare a variable named 'firstInteger' and initialize with integer value 4.
    const firstInteger = 4;

    // Declare a variable named 'firstDecimal' and initialize with floating-point value 4.0.
    const firstDecimal = 4.0;

    // Declare a variable named 'firstString' and initialize with the string "HackerRank".
    const firstString = 'HackerRank ';

    // Write code that uses console.log to print the sum of the 'firstInteger' and 'secondInteger' (converted to a Number        type) on a new line.


    // Write code that uses console.log to print the sum of 'firstDecimal' and 'secondDecimal' (converted to a Number            type) on a new line.


    // Write code that uses console.log to print the concatenation of 'firstString' and 'secondString' on a new line. The        variable 'firstString' must be printed first.

}
Enter fullscreen mode Exit fullscreen mode
function main() {
    const secondInteger = readLine();
    const secondDecimal = readLine();
    const secondString = readLine();

    performOperation(secondInteger, secondDecimal, secondString);
}
Enter fullscreen mode Exit fullscreen mode

Top comments (3)

Collapse
 
lakshyatyagi24 profile image
Lakshya Tyagi • Edited
    function performOperation(secondInteger, secondDecimal, secondString) {
        const firstInteger = 4;
        const firstDecimal = 4.0;
        const firstString = 'HackerRank ';

        const sum=firstInteger+parseInt(secondInteger);
        console.log(sum)

         console.log(firstDecimal+parseFloat(secondDecimal))

        console.log(firstString+secondString)       
    }
Enter fullscreen mode Exit fullscreen mode
Collapse
 
joachimzeelmaekers profile image
Joachim Zeelmaekers

Nice read! If you want to improve the readability you can use js to the first 3 backticks to add syntax highlighting! It would look like this:

function main() {
    const secondInteger = readLine();
    const secondDecimal = readLine();
    const secondString = readLine();

    performOperation(secondInteger, secondDecimal, secondString);
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
lakshyatyagi24 profile image
Lakshya Tyagi

Thanks Joachim Today i get to know something knew and it's really interesting