From 75d09d85e1d5ed388552dfaf699f1744a3da69e7 Mon Sep 17 00:00:00 2001 From: Colleen Kingsley Date: Thu, 23 Jul 2020 19:03:10 -0400 Subject: [PATCH 1/4] Declare grades array and make code exportable --- data/grades.js | 1 + index.js | 42 +++++++++++++++++++++++++++--------------- 2 files changed, 28 insertions(+), 15 deletions(-) create mode 100644 data/grades.js diff --git a/data/grades.js b/data/grades.js new file mode 100644 index 0000000..7707db0 --- /dev/null +++ b/data/grades.js @@ -0,0 +1 @@ +const grades = require('./data/index') \ No newline at end of file diff --git a/index.js b/index.js index 856a122..4d73da0 100644 --- a/index.js +++ b/index.js @@ -15,7 +15,15 @@ Create a new file named "grades.js" to import in the data folder Declare and initialize an array named `grades` - + */ +const grades = [ + { assignmentName: 'Assignment 01', studentName: 'Ben', score: 50 }, + { assignmentName: 'Assignment 02', studentName: 'Jerry', score: 80 }, + { assignmentName: 'Assignment 02', studentName: 'Ben', score: 95 }, + { assignmentName: 'Assignment 03', studentName: 'Jerry', score: 100 }, + { assignmentName: 'Assignment 01', studentName: 'Ben', score: 70 } +] +/** Include at least 5 object items in the array Each object will have properties of @@ -25,29 +33,33 @@ Values are important for this lesson. - - Use 2 distinct assignment names - - Use 2 distinct student names + - Use distinct assignment names + - Use only 2 distinct student names */ - /** +/** - 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 +*/ +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 + +module.exports = { + grades +} \ No newline at end of file From 25acd49c030756fee7c5e8f150cd2576407a20c5 Mon Sep 17 00:00:00 2001 From: Colleen Kingsley Date: Thu, 23 Jul 2020 19:06:34 -0400 Subject: [PATCH 2/4] Update array name to studentGrades --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 4d73da0..2b3f755 100644 --- a/index.js +++ b/index.js @@ -16,7 +16,7 @@ Declare and initialize an array named `grades` */ -const grades = [ +const studentGrades = [ { assignmentName: 'Assignment 01', studentName: 'Ben', score: 50 }, { assignmentName: 'Assignment 02', studentName: 'Jerry', score: 80 }, { assignmentName: 'Assignment 02', studentName: 'Ben', score: 95 }, From 6e48d9357301371178ee9d320600bfaf69eebf3a Mon Sep 17 00:00:00 2001 From: Colleen Kingsley Date: Thu, 23 Jul 2020 19:58:22 -0400 Subject: [PATCH 3/4] Create for loop totaling student grades --- data/grades.js | 2 +- index.js | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/data/grades.js b/data/grades.js index 7707db0..50409a5 100644 --- a/data/grades.js +++ b/data/grades.js @@ -1 +1 @@ -const grades = require('./data/index') \ No newline at end of file +const { grades } = require('./index.js') \ No newline at end of file diff --git a/index.js b/index.js index 2b3f755..3ef2881 100644 --- a/index.js +++ b/index.js @@ -16,7 +16,7 @@ Declare and initialize an array named `grades` */ -const studentGrades = [ +const grades = [ { assignmentName: 'Assignment 01', studentName: 'Ben', score: 50 }, { assignmentName: 'Assignment 02', studentName: 'Jerry', score: 80 }, { assignmentName: 'Assignment 02', studentName: 'Ben', score: 95 }, @@ -46,8 +46,21 @@ const studentGrades = [ Update `gradeTotal` so it increases value for each item in the array */ -const gradeTotal = 0 +/* let gradeTotal = 0 +for (const index in grades) { + // destructuring score + const {score } = grades[index] + console.log({score}) + gradeTotal += gradeTotal +} +console.log({gradeTotal}) */ + +gradeTotal = 0 +grades.forEach( grade => { + gradeTotal += grade.score +}) +console.log( {gradeTotal} ) /** From 3470c133e5af1dc5ec84b41deb061b76ee7f0db7 Mon Sep 17 00:00:00 2001 From: Colleen Kingsley Date: Thu, 23 Jul 2020 20:17:23 -0400 Subject: [PATCH 4/4] Use the reduce function to add all grades --- index.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 3ef2881..7109af0 100644 --- a/index.js +++ b/index.js @@ -55,12 +55,13 @@ for (const index in grades) { gradeTotal += gradeTotal } console.log({gradeTotal}) */ - +/* gradeTotal = 0 grades.forEach( grade => { gradeTotal += grade.score -}) -console.log( {gradeTotal} ) +console.log( {gradeTotal} ) */ + + /** @@ -71,7 +72,12 @@ console.log( {gradeTotal} ) Replace `null` below with the use of `reduce` */ -gradeTotal = null +let gradeTotal = grades.reduce((total, grade) => { + //use console.log to make sure you are looking at the correct variables/properties + console.log(total, grade) + total += grade.score + return total +}, 0) module.exports = { grades