@@ -10,36 +10,12 @@ class grades {
10
10
11
11
displayAvg ( ) {
12
12
let sum = 0 ;
13
- for ( const key in this . grades ) {
14
- sum += this . grades [ key ] ;
15
- }
13
+ this . grades . forEach ( grade => sum += grade ) ;
16
14
return sum / this . grades . length ;
17
15
}
18
-
19
16
}
20
17
21
- // Excercises
22
- console . log ( 'CHAPTER 2' ) ;
23
- console . log ( '### Excercise 1' ) ;
24
-
25
- const gradeObj = [ 90 , 89 , 75 ] ;
26
- const grade = new grades ( gradeObj ) ;
27
- grade . addGrade ( 90 ) ;
28
- grade . addGrade ( 25 ) ;
29
- console . log ( `Avg grade: ${ grade . displayAvg ( ) } ` ) ;
30
-
31
- // #############################################
32
-
33
- console . log ( '\n' ) ;
34
- console . log ( '### Excercise 2' ) ;
35
-
36
- const arrayWords = [ "hello " , "my " , "friend " ] ;
37
- console . log ( `orginal array: ${ arrayWords } ` ) ;
38
- console . log ( `displaying forward: ${ arrayWords . reduce ( ( total , word ) => total + word ) } ` ) ;
39
- console . log ( `displaying backward: ${ arrayWords . reduceRight ( ( total , word ) => total + word ) } ` ) ;
40
-
41
- // #############################################
42
-
18
+ // ##############################################
43
19
class weekTemps {
44
20
45
21
constructor ( dataStore = [ ] ) {
@@ -53,38 +29,55 @@ class weekTemps {
53
29
averageWeek ( week ) {
54
30
let total = 0 ;
55
31
let totalDays = this . dataStore [ week ] . length ;
56
- for ( let i = 0 ; i < totalDays ; i ++ ) {
57
- total += this . dataStore [ week ] [ i ] ;
58
- }
32
+
33
+ this . dataStore [ week ] . forEach ( item => total += item ) ;
34
+
59
35
return ( total / totalDays ) . toFixed ( 2 ) ;
60
36
}
61
37
62
38
displayMonthAvg ( ) {
63
39
let sum = 0 ;
64
- for ( const key in this . dataStore ) {
65
- for ( const key2 in this . dataStore [ key ] ) {
66
- sum += this . dataStore [ key ] [ key2 ] ;
67
- }
68
- }
40
+
41
+ this . dataStore . forEach ( week =>
42
+ week . forEach ( day => sum += day )
43
+ ) ;
44
+
69
45
return ( sum / ( this . dataStore . length * 4 ) ) . toFixed ( 2 ) ;
70
46
}
71
47
72
48
displayAllWeekAvg ( ) {
73
49
let sum = 0 ;
74
- let avg = [ ] ;
75
- for ( const key in this . dataStore ) {
76
- for ( const key2 in this . dataStore [ key ] ) {
77
- sum += this . dataStore [ key ] [ key2 ] ;
78
- }
79
- avg [ key ] = `Week ${ parseInt ( key , 10 ) + 1 } Temp Avg: ${ ( sum / this . dataStore [ key ] . length ) . toFixed ( 2 ) } ` ;
80
- sum = 0 ;
81
- }
82
- return avg ;
83
- }
84
50
51
+ this . dataStore . forEach ( ( week , count ) => {
52
+ week . forEach ( day => sum += day ) ;
53
+ console . log ( `Week ${ count + 1 } Temp Avg: ${ ( sum / week . length ) . toFixed ( 2 ) } ` ) ;
54
+ sum = 0 ;
55
+ } ) ;
56
+
57
+ }
85
58
}
86
- console . log ( '\n' ) ;
87
- console . log ( '### Excercise 3' ) ;
59
+
60
+ // Excercises
61
+ // #############################################
62
+ console . log ( 'CHAPTER 2' ) ;
63
+ console . log ( '### Excercise 1' ) ;
64
+
65
+ const gradeObj = [ 90 , 89 , 75 ] ;
66
+ const grade = new grades ( gradeObj ) ;
67
+ grade . addGrade ( 90 ) ;
68
+ grade . addGrade ( 25 ) ;
69
+ console . log ( `Avg grade: ${ grade . displayAvg ( ) } ` ) ;
70
+
71
+ // #############################################
72
+ console . log ( '\n### Excercise 2' ) ;
73
+
74
+ const arrayWords = [ "hello " , "my " , "friend " ] ;
75
+ console . log ( `orginal array: ${ arrayWords } ` ) ;
76
+ console . log ( `displaying forward: ${ arrayWords . reduce ( ( total , word ) => total + word ) } ` ) ;
77
+ console . log ( `displaying backward: ${ arrayWords . reduceRight ( ( total , word ) => total + word ) } ` ) ;
78
+
79
+ // #############################################
80
+ console . log ( '\n### Excercise 3' ) ;
88
81
89
82
const randomMonth = [
90
83
[ 45 , 23 , 32 , 12 , 31 , 21 , 22 ] ,
@@ -96,16 +89,11 @@ const thisMonth = new weekTemps(randomMonth);
96
89
97
90
console . log ( `Month Temp avg: ${ thisMonth . displayMonthAvg ( ) } ` ) ;
98
91
console . log ( `Week 2 Temp avg: ${ thisMonth . averageWeek ( 2 ) } ` ) ;
99
- console . log ( `Week's Temp avg:` ) ;
100
- const weeks = thisMonth . displayAllWeekAvg ( ) ;
101
- for ( const key in weeks ) {
102
- console . log ( weeks [ key ] ) ;
103
- }
92
+ console . log ( `\nDisplay All Week Avg` ) ;
93
+ thisMonth . displayAllWeekAvg ( ) ;
104
94
105
95
// #############################################
106
-
107
- console . log ( '\n' ) ;
108
- console . log ( '### Excercise 4' ) ;
96
+ console . log ( '\n### Excercise 4' ) ;
109
97
110
98
const letters = [ 'a' , 'b' , 'a' , 'c' , 'a' , 'd' , 'a' , 'b' , 'r' , 'a' ] ;
111
99
console . log ( letters . reduce ( ( all , letter ) => all + letter ) ) ;
0 commit comments