Skip to content

Commit 01c04ce

Browse files
committed
exception exercises
1 parent 0f35d61 commit 01c04ce

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

exceptions/exercises/divide.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
// Write a function called 'divide' that takes two parameters: a numerator and a denominator.
2+
function divide(numerator, denominator) {
3+
if (denominator === 0) {
4+
throw Error('Attempted to divide by zero.');
5+
}
6+
return numerator / denominator;
7+
}
28

39
// Your function should return the result of numerator / denominator.
410

exceptions/exercises/test-student-labs.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
function gradeLabs(labs) {
22
for (let i=0; i < labs.length; i++) {
33
let lab = labs[i];
4-
let result = lab.runLab(3);
4+
let result;
5+
try {
6+
result = lab.runLab(3);
7+
} catch (error) {
8+
result = 'Error thrown';
9+
}
10+
511
console.log(`${lab.student} code worked: ${result === 27}`);
612
}
713
}
@@ -21,4 +27,27 @@ let studentLabs = [
2127
}
2228
];
2329

30+
let studentLabs2 = [
31+
{
32+
student: 'Blake',
33+
myCode: function (num) {
34+
return Math.pow(num, num);
35+
}
36+
},
37+
{
38+
student: 'Jessica',
39+
runLab: function (num) {
40+
return Math.pow(num, num);
41+
}
42+
},
43+
{
44+
student: 'Mya',
45+
runLab: function (num) {
46+
return num * num;
47+
}
48+
}
49+
];
50+
2451
gradeLabs(studentLabs);
52+
53+
gradeLabs(studentLabs2);

0 commit comments

Comments
 (0)