File tree 2 files changed +53
-9
lines changed
2 files changed +53
-9
lines changed Original file line number Diff line number Diff line change 1
1
// Write a function called 'divide' that takes two parameters: a numerator and a denominator.
2
+ let a = 10 ;
3
+ let b = 0 ;
2
4
5
+ function divide ( numerator , denominator ) {
6
+ if ( denominator === 0 ) {
7
+ throw Error ( 'Attempted to divide by zero.' ) ;
8
+
9
+ } else {
10
+ return numerator / denominator ;
11
+ }
12
+
13
+ }
14
+
15
+ console . log ( divide ( a , b ) ) ;
3
16
// Your function should return the result of numerator / denominator.
4
17
5
18
// However, if the denominator is zero you should throw the error, "Attempted to divide by zero."
Original file line number Diff line number Diff line change 1
1
function gradeLabs ( labs ) {
2
2
for ( let i = 0 ; i < labs . length ; i ++ ) {
3
- let lab = labs [ i ] ;
4
- let result = lab . runLab ( 3 ) ;
5
- console . log ( `${ lab . student } code worked: ${ result === 27 } ` ) ;
3
+ let lab = labs [ i ] ;
4
+ let result ;
5
+
6
+ try {
7
+ result = lab . run . Lab ( 3 ) ;
8
+ console . log ( `${ lab . student } code worked: ${ result === 27 } ` ) ;
9
+ }
10
+ catch ( error ) {
11
+ result = "Error thrown" ;
12
+ console . log ( result ) ;
13
+ }
6
14
}
7
15
}
8
16
9
17
let studentLabs = [
10
18
{
11
- student : 'Carly' ,
12
- runLab : function ( num ) {
19
+ student : 'Carly' ,
20
+ runLab : function ( num ) {
13
21
return Math . pow ( num , num ) ;
14
- }
22
+ }
15
23
} ,
16
24
{
17
- student : 'Erica' ,
18
- runLab : function ( num ) {
25
+ student : 'Erica' ,
26
+ runLab : function ( num ) {
19
27
return num * num ;
20
- }
28
+ }
21
29
}
22
30
] ;
23
31
24
32
gradeLabs ( studentLabs ) ;
33
+
34
+ let studentLabs2 = [
35
+ {
36
+ student : 'Blake' ,
37
+ myCode : function ( num ) {
38
+ return Math . pow ( num , num ) ;
39
+ }
40
+ } ,
41
+ {
42
+ student : 'Jessica' ,
43
+ runLab : function ( num ) {
44
+ return Math . pow ( num , num ) ;
45
+ }
46
+ } ,
47
+ {
48
+ student : 'Mya' ,
49
+ runLab : function ( num ) {
50
+ return num * num ;
51
+ }
52
+ }
53
+ ] ;
54
+
55
+ gradeLabs ( studentLabs2 ) ;
You can’t perform that action at this time.
0 commit comments