Day 3: Try, Catch, and Finally (10 Days of Javascript)

Task

Complete the reverseString function; it has one parameter, s. You must perform the following actions:

  1. Try to reverse string s using the splitreverse, and join methods.
  2. If an exception is thrown, catch it and print the contents of the exception’s message on a new line.
  3. Print s on a new line. If no exception was thrown, then this should be the reversed string; if an exception was thrown, this should be the original string.


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 reverseString function
 * Use console.log() to print to stdout.
 */
function reverseString(s) {
    let str=s;
    try{
        str=s.split("").reverse().join("");
    }catch(error){
        console.log(error.message);
    }
    finally{
        console.log(str);
    }
}

function main() {
    const s = eval(readLine());
    
    reverseString(s);
}

Leave a Reply