@@ -15,14 +15,62 @@ class grades {
15
15
}
16
16
}
17
17
18
- // Implementation of the grade class
18
+ // Implementation of the grade using classes
19
19
console . log ( '### Excercise 1' ) ;
20
- const gradeObj = [ 90 , 89 , 75 ] ;
20
+ console . log ( '## Using ES6 Class, using the new keyword' ) ;
21
+ const gradeObj = [ 90 , 89 , 75 , 90 , 89 , 75 , 90 , 89 , 75 , 90 , 89 , 75 , 90 , 89 , 75 , 90 , 89 , 75 ] ;
21
22
const grade = new grades ( gradeObj ) ;
22
23
grade . addGrade ( 90 ) ;
23
24
grade . addGrade ( 25 ) ;
24
25
console . log ( `Avg grade: ${ grade . displayAvg ( ) } ` ) ;
25
26
27
+
28
+ const gradesProto = {
29
+ init ( grades = [ ] ) {
30
+ this . grades = grades ;
31
+ return this ;
32
+ } ,
33
+ addGrade ( grade ) {
34
+ this . grades . push ( grade ) ;
35
+ } ,
36
+ displayAvg ( ) {
37
+ let sum = this . grades . reduce ( ( grades , grade ) => grades += grade , 0 ) ;
38
+ return sum / this . grades . length ;
39
+ }
40
+ }
41
+
42
+ // Implementation of the grade prototype
43
+ console . log ( '\n## Using prototype, using Object.create()' ) ;
44
+ const gradeO = [ 90 , 89 , 75 , 90 , 89 , 75 , 90 , 89 , 75 , 90 , 89 , 75 , 90 , 89 , 75 , 90 , 89 , 75 ] ;
45
+ const gradeP = Object . create ( gradesProto ) . init ( gradeO ) ;
46
+ gradeP . addGrade ( 90 ) ;
47
+ gradeP . addGrade ( 25 ) ;
48
+ console . log ( `Avg grade: ${ gradeP . displayAvg ( ) } ` ) ;
49
+
50
+ const gradesFactory = ( ) => {
51
+ let grades = [ ] ;
52
+ return {
53
+ init ( g = [ ] ) {
54
+ grades = g ;
55
+ return this ;
56
+ } ,
57
+ addGrade ( grade ) {
58
+ grades . push ( grade ) ;
59
+ } ,
60
+ displayAvg ( ) {
61
+ let sum = grades . reduce ( ( g , grade ) => g += grade , 0 ) ;
62
+ return sum / grades . length ;
63
+ }
64
+ }
65
+ }
66
+
67
+ // Implementation of the grade factory
68
+ console . log ( '\n## Using prototype, using factory' ) ;
69
+ const grade3 = gradesFactory ( ) . init ( gradeO ) ;
70
+ grade3 . addGrade ( 90 ) ;
71
+ grade3 . addGrade ( 25 ) ;
72
+ console . log ( `Avg grade: ${ grade3 . displayAvg ( ) } ` ) ;
73
+
26
74
// #############################################
27
75
console . log ( '\n### Excercise 2' ) ;
28
76
const arrayWords = [ "hello " , "my " , "friend " ] ;
@@ -88,6 +136,48 @@ console.log(`Week 2 Temp avg: ${thisMonth.averageWeek(2)}`);
88
136
console . log ( `\nDisplay All Week Avg` ) ;
89
137
thisMonth . displayAllWeekAvg ( ) ;
90
138
139
+
140
+ const weekTempsProto = {
141
+ init ( dataStore = [ ] ) {
142
+ this . dataStore = dataStore
143
+ return this
144
+ } ,
145
+ add ( temp ) {
146
+ this . dataStore . push ( temp )
147
+ } ,
148
+ averageWeek ( week ) {
149
+ let totalDays = this . dataStore [ week ] . length
150
+ let total = this . dataStore [ week ] . reduce ( ( week , day ) => week += day , 0 )
151
+ return ( total / totalDays ) . toFixed ( 2 )
152
+ } ,
153
+ displayMonthAvg ( ) {
154
+ let sum = this . dataStore . reduce ( ( weeks , week ) =>
155
+ weeks + week . reduce ( ( days , day ) => days + day , 0 )
156
+ , 0 )
157
+ return ( sum / ( this . dataStore . length * 4 ) ) . toFixed ( 2 )
158
+ } ,
159
+ displayAllWeekAvg ( ) {
160
+ let sum = 0
161
+ this . dataStore . forEach ( ( week , count ) => {
162
+ week . forEach ( day => sum += day )
163
+ console . log ( `Week ${ count + 1 } Temp Avg: ${ ( sum / week . length ) . toFixed ( 2 ) } ` )
164
+ sum = 0
165
+ } ) ;
166
+ }
167
+ } ;
168
+
169
+ // Implementation of weekTemps prototype object
170
+ console . log ( '\n## Using prototype' )
171
+
172
+ const month = Object . create ( weekTempsProto ) . init ( randomMonth )
173
+
174
+ console . log ( `Month Temp avg: ${ month . displayMonthAvg ( ) } ` )
175
+ console . log ( `Week 2 Temp avg: ${ month . averageWeek ( 2 ) } ` )
176
+ console . log ( `\nDisplay All Week Avg` )
177
+ month . displayAllWeekAvg ( )
178
+
179
+ console . log ( JSON . stringify ( weekTempsProto , null , 2 ) ) ;
180
+
91
181
// #############################################
92
182
console . log ( '\n### Excercise 4' ) ;
93
183
0 commit comments