Skip to content

Commit 7e067ab

Browse files
committed
exeptions exercises completed
1 parent 71cd477 commit 7e067ab

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

exceptions/exercises/divide.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
// Write a function called 'divide' that takes two parameters: a numerator and a denominator.
2-
2+
// function divide(numerator, denominator) {
3+
// return numerator / demoninator
4+
// }
35
// Your function should return the result of numerator / denominator.
46

57
// However, if the denominator is zero you should throw the error, "Attempted to divide by zero."
68

79
// Code your divide function here:
10+
function divide(numerator, denominator) {
11+
if (denominator === 0) {
12+
throw Error('You cannot divide by zero!')
13+
}
14+
return numerator / denominator
15+
}
16+
console.log(divide(4,0))

exceptions/exercises/test-student-labs.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
function gradeLabs(labs) {
22
for (let i=0; i < labs.length; i++) {
33
let lab = labs[i];
4+
try {
45
let result = lab.runLab(3);
56
console.log(`${lab.student} code worked: ${result === 27}`);
7+
} catch(err) {
8+
console.log('Error thrown')
69
}
710
}
11+
}
812

913
let studentLabs = [
1014
{
@@ -22,3 +26,26 @@ let studentLabs = [
2226
];
2327

2428
gradeLabs(studentLabs);
29+
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+
51+
gradeLabs(studentLabs2)

0 commit comments

Comments
 (0)