Skip to content

Commit 91ca5b4

Browse files
committed
chapter 2 code refactored
1 parent a1de95e commit 91ca5b4

File tree

1 file changed

+42
-54
lines changed

1 file changed

+42
-54
lines changed

02-chapter-Arrays.js

Lines changed: 42 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,12 @@ class grades {
1010

1111
displayAvg(){
1212
let sum = 0;
13-
for (const key in this.grades) {
14-
sum += this.grades[key];
15-
}
13+
this.grades.forEach(grade => sum += grade );
1614
return sum / this.grades.length;
1715
}
18-
1916
}
2017

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+
// ##############################################
4319
class weekTemps {
4420

4521
constructor(dataStore = []) {
@@ -53,38 +29,55 @@ class weekTemps {
5329
averageWeek(week) {
5430
let total = 0;
5531
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+
5935
return (total / totalDays).toFixed(2);
6036
}
6137

6238
displayMonthAvg() {
6339
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+
6945
return (sum / (this.dataStore.length * 4)).toFixed(2);
7046
}
7147

7248
displayAllWeekAvg() {
7349
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-
}
8450

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+
}
8558
}
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');
8881

8982
const randomMonth = [
9083
[45,23,32,12,31,21,22],
@@ -96,16 +89,11 @@ const thisMonth = new weekTemps(randomMonth);
9689

9790
console.log(`Month Temp avg: ${thisMonth.displayMonthAvg()}`);
9891
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();
10494

10595
// #############################################
106-
107-
console.log('\n');
108-
console.log('### Excercise 4');
96+
console.log('\n### Excercise 4');
10997

11098
const letters = ['a','b','a','c','a','d','a','b','r','a'];
11199
console.log(letters.reduce((all, letter) => all + letter));

0 commit comments

Comments
 (0)