From 904e035b574fec4e356a82a33b9bc5cdf86aa74f Mon Sep 17 00:00:00 2001 From: Stephanie Bucharelli Date: Thu, 23 Jul 2020 18:53:13 -0400 Subject: [PATCH 1/8] Create grades array for reduce lesson --- data/grades.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 data/grades.js diff --git a/data/grades.js b/data/grades.js new file mode 100644 index 0000000..ced9446 --- /dev/null +++ b/data/grades.js @@ -0,0 +1,30 @@ +grades = [{ + assignmentName: Assignment01, + studentName: Timmy, + score: 90 +}, +{ + assignmentName: Assignment01, + studentName: Zoey, + score: 95 +}, +{ + assignmentName: Assignment02, + studentName: Timmy, + score: 85 +}, +{ + assignmentName: Assignment02, + studentName: Zoey, + score: 70 +}, +{ + assignmentName: Assignment03, + studentName: Barry, + score: 100 +}, +{ + assignmentName: Assignment03, + studentName: Timmy, + score: 60 +}] \ No newline at end of file From f5589dbf5e5213fb2e4b7d47913ad5304f3425a0 Mon Sep 17 00:00:00 2001 From: Stephanie Bucharelli Date: Thu, 23 Jul 2020 18:56:29 -0400 Subject: [PATCH 2/8] Fix array - add strings and use only 2 names --- data/grades.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/data/grades.js b/data/grades.js index ced9446..90414ac 100644 --- a/data/grades.js +++ b/data/grades.js @@ -1,30 +1,30 @@ grades = [{ - assignmentName: Assignment01, - studentName: Timmy, + assignmentName: 'Assignment01', + studentName: 'Timmy', score: 90 }, { - assignmentName: Assignment01, - studentName: Zoey, + assignmentName: 'Assignment01', + studentName: 'Zoey', score: 95 }, { - assignmentName: Assignment02, - studentName: Timmy, + assignmentName: 'Assignment02', + studentName: 'Timmy', score: 85 }, { - assignmentName: Assignment02, - studentName: Zoey, + assignmentName: 'Assignment02', + studentName: 'Zoey', score: 70 }, { - assignmentName: Assignment03, - studentName: Barry, + assignmentName: 'Assignment03', + studentName: 'Zoey', score: 100 }, { - assignmentName: Assignment03, - studentName: Timmy, + assignmentName: 'Assignment03', + studentName: 'Timmy', score: 60 }] \ No newline at end of file From 18fa10b4891c5035de361e7fbc894edae1597619 Mon Sep 17 00:00:00 2001 From: Stephanie Bucharelli Date: Thu, 23 Jul 2020 18:59:14 -0400 Subject: [PATCH 3/8] Add module export for grades --- data/grades.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/data/grades.js b/data/grades.js index 90414ac..785200c 100644 --- a/data/grades.js +++ b/data/grades.js @@ -27,4 +27,8 @@ grades = [{ assignmentName: 'Assignment03', studentName: 'Timmy', score: 60 -}] \ No newline at end of file +}] + +module.exports = { + grades +} \ No newline at end of file From 8f645f57dd3e89c3196bf17c37035f4a9bb04c74 Mon Sep 17 00:00:00 2001 From: Stephanie Bucharelli Date: Thu, 23 Jul 2020 19:11:09 -0400 Subject: [PATCH 4/8] Add require function to index.js to reference grades --- index.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index 856a122..34a28ec 100644 --- a/index.js +++ b/index.js @@ -29,25 +29,25 @@ - Use 2 distinct student names */ +const { grades } = require('./data/grades') +/** - /** - - Looping using your preferred looping syntax and updating a shared variable + Looping using your preferred looping syntax and updating a shared variable - 2) Loop through the grades data using a for loop. - Update `gradeTotal` so it increases value for each item in the array + 2) Loop through the grades data using a for loop. + Update `gradeTotal` so it increases value for each item in the array - */ - const gradeTotal = 0 +*/ - /** + +/** - Using reduce + Using reduce - 3) Use Array reduce to do the same total calculation logic + 3) Use Array reduce to do the same total calculation logic - Replace `null` below with the use of `reduce` + Replace `null` below with the use of `reduce` - */ - gradeTotal = null + */ +gradeTotal = null From 0395b5f028c57cb8aac418237f6dcb95540fef05 Mon Sep 17 00:00:00 2001 From: Stephanie Bucharelli Date: Thu, 23 Jul 2020 19:19:36 -0400 Subject: [PATCH 5/8] Loop through grades and total scores --- index.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 34a28ec..de8eebe 100644 --- a/index.js +++ b/index.js @@ -38,7 +38,16 @@ const { grades } = require('./data/grades') Update `gradeTotal` so it increases value for each item in the array */ - +function totalGrades(grades) { + let gradeTotal = 0 + grades.forEach((grade) => { + const { score } = grade + gradeTotal += score + }) + return gradeTotal +} + +console.log(totalGrades(grades)) /** From fc043f35c07421877dbaa2a4633425ebcae550fc Mon Sep 17 00:00:00 2001 From: Stephanie Bucharelli Date: Thu, 23 Jul 2020 19:48:53 -0400 Subject: [PATCH 6/8] Add for...in loop options --- index.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/index.js b/index.js index de8eebe..18a400c 100644 --- a/index.js +++ b/index.js @@ -30,6 +30,8 @@ */ const { grades } = require('./data/grades') + +console.log({ grades }) /** Looping using your preferred looping syntax and updating a shared variable @@ -38,6 +40,7 @@ const { grades } = require('./data/grades') Update `gradeTotal` so it increases value for each item in the array */ +// expect gradeTotal = 500 function totalGrades(grades) { let gradeTotal = 0 grades.forEach((grade) => { @@ -49,7 +52,23 @@ function totalGrades(grades) { console.log(totalGrades(grades)) +// add in for...in loop option 1 +gradeTotal = 0 + +for (const index in grades) { + const { score } = grades[index] + console.log({ score }) + gradeTotal += score +} +console.log({ gradeTotal }) +gradeTotal = 0 + +for (const index in grades) { + const grade = grades[index] + gradeTotal += grade.score +} +console.log({ gradeTotal }) /** Using reduce From 421db571b81945c564f21c348ad1602c6c83e55d Mon Sep 17 00:00:00 2001 From: Stephanie Bucharelli Date: Thu, 23 Jul 2020 20:20:59 -0400 Subject: [PATCH 7/8] Create reduce function to total grades --- index.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 18a400c..4f9bbbd 100644 --- a/index.js +++ b/index.js @@ -31,7 +31,7 @@ */ const { grades } = require('./data/grades') -console.log({ grades }) +// console.log({ grades }) /** Looping using your preferred looping syntax and updating a shared variable @@ -78,4 +78,12 @@ console.log({ gradeTotal }) Replace `null` below with the use of `reduce` */ -gradeTotal = null +function totalGrades(grades) { + const gradeTotal = grades.reduce((total, grade) => { + console.log(total, grade) + return total + grade.score + }, 0) + return gradeTotal +} + +console.log(totalGrades(grades)) \ No newline at end of file From 06529245f41c15bd4e865ac7ef906eabfcb304a4 Mon Sep 17 00:00:00 2001 From: Stephanie Bucharelli Date: Thu, 23 Jul 2020 20:24:05 -0400 Subject: [PATCH 8/8] Include destructered version for reduce f'n --- index.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 4f9bbbd..e5b1c83 100644 --- a/index.js +++ b/index.js @@ -86,4 +86,15 @@ function totalGrades(grades) { return gradeTotal } -console.log(totalGrades(grades)) \ No newline at end of file +console.log(totalGrades(grades)) + +function totalGradesAlt(grades) { + const gradeTotal = grades.reduce((total, grade) => { + console.log(total, grade) + const { score } = grade + return total + score + }, 0) + return gradeTotal +} + +console.log(totalGradesAlt(grades)) \ No newline at end of file