-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathday3-try-catch-and-finally.js
More file actions
30 lines (26 loc) · 1.21 KB
/
Copy pathday3-try-catch-and-finally.js
File metadata and controls
30 lines (26 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
Objective
In this challenge, we learn about strings and exceptions. Check out the attached tutorials for more details.
Task
Complete the reverseString function; it has one parameter, s. You must perform the following actions:
- Try to reverse string s using the split, reverse, and join methods.
- If an exception is thrown, catch it and print the contents of the exception's message on a new line.
- 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.
Input Format
Locked stub code in the editor reads variable s from stdin and passes it to the function.
Output Format
You must write two print statements using console.log():
- Print the contents of a caught exception's message on a new line. If no exception was thrown, this line should not be printed.
- 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.
*/
function reverseString(s) {
try {
let arr = s.split("");
arr.reverse();
s = arr.join("");
} catch (e) {
console.log(e.message);
} finally {
console.log(s);
}
}