코딩테스트/HackerRank

HackerRank day3 - Throw

무지짱 2022. 12. 14. 15:45
반응형

정수이면 YES, 0 이면 Zero Error, 음수이면 Negative Error를 출력해야한다.

처음에는 return으로 출력 시켰지만 Throw를 사용하라는 것 같아서

사용법을 찾아보고 다음과 같이 풀었다.

 

'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){
        throw new Error('Zero Error');
    }else if(a < 0){
        throw new Error('Negative Error');
    }else{
        return 'YES'
    }
}
반응형