Day 16: Exceptions - String to Integer

  • + 0 comments

    Hi Can someone explain why this makes the test to fail (while the output is ok..) ? Thanks in advance

    'use strict';
    
    process.stdin.resume();
    process.stdin.setEncoding('utf-8');
    
    let inputString = '';
    let currentLine = 0;
    
    process.stdin.on('data', function(inputStdin) {
        inputString += inputStdin;
    });
    
    process.stdin.on('end', function() {
        inputString = inputString.split('\n');
    
        main();
    });
    
    function readLine() {
        return inputString[currentLine++];
    }
    
    
    
    function main() {
        const S = readLine();
        
         try {
            const parsed = parseInt(S, 10)
            if (!/^\d+$/.test(S) || isNaN(parsed)) {
                throw new Error("Bad String");
            }
            console.log(parsed);
        } catch (err) {
            console.log("Bad String");
        }
    }