Header Ad

HackerRank Day 3: Throw 10 days of javascript solution

In this HackerRank Day 3: Throw 10 Days of javascript problem you need to complete the isPositive function below. It has one integer parameter, a. If the value of a is positive, it must return the string YES. Otherwise, If a is 0, throw an Error with message=Zero Error. If a is negative, throw an Error with message=Negative Error.

HackerRank Day 3: Throw 10 days of javascript solution


HackerRank Day 3: Throw 10 days of javascript problem 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++];
}

/*
 * Complete the isPositive function.
 * If 'a' is positive, return "YES".
 * If 'a' is 0, throw an Error with the message "Zero Error"
 * If 'a' is negative, throw an Error with the message "Negative Error"
 */
function isPositive(a) {
    if(a > 0){
        return 'YES';
    }
    else{
        throw (a === 0 ? new Error('Zero Error') : new Error('Negative Error'));
    }
}


function main() {
    const n = +(readLine());
    
    for (let i = 0; i < n; i++) {
        const a = +(readLine());
      
        try {
            console.log(isPositive(a));
        } catch (e) {
            console.log(e.message);
        }
    }
}


Post a Comment

0 Comments