-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path045-Error_Handle.js
41 lines (38 loc) · 1.04 KB
/
045-Error_Handle.js
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
31
32
33
34
35
36
37
38
39
40
41
/*
* Author : Jaydatt Patel
error handle Sample program
*/
console.log("-----------Program-1-------------");
function addTwoNums(a, b) {
try {
if (typeof a != "number") {
throw new ReferenceError("the first argument is not a number");
} else if (typeof b != "number") {
throw new ReferenceError("the second argument is not a number");
} else {
console.log(a + b);
}
} catch (err) {
console.log("Error!", err);
}
}
addTwoNums(5, "5");
console.log("It still works");
console.log("-----------Program-2-------------");
function letterFinder(word, match) {
if (
typeof word == "string" &&
word.length >= 2 &&
typeof match == "string" &&
match.length == 1
) {
for (var i = 0; i < word.length; i++) {
if (word[i] == match) console.log("Found the", match, "at", i);
else console.log("---No match found at", i);
}
} else {
console.log("Please pass correct arguments to the function.");
}
}
letterFinder(5, 2);
letterFinder("cat", "c");