Skip to content

Commit 68f83c4

Browse files
committed
exceptionsexercise
1 parent 589e08e commit 68f83c4

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

exceptions/exercises/divide.js

Lines changed: 9 additions & 0 deletions
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+
23

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("Attempted to divide by zero.");
13+
}
14+
return numerator/denominator;
15+
}
16+
console.log(divide(5,0));

exceptions/exercises/test-student-labs.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
function gradeLabs(labs) {
2+
try{
23
for (let i=0; i < labs.length; i++) {
34
let lab = labs[i];
45
let result = lab.runLab(3);
56
console.log(`${lab.student} code worked: ${result === 27}`);
67
}
8+
}catch(err){
9+
console.log("Error thrown") ;
10+
}
711
}
812

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

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

0 commit comments

Comments
 (0)