diff --git a/data/grades.js b/data/grades.js new file mode 100644 index 0000000..acfb4ac --- /dev/null +++ b/data/grades.js @@ -0,0 +1,30 @@ + +const grades = [{ + assignmentName: 'Assignment 1', + studentName: 'Nicholas', + score: 75 +}, { + assignmentName: 'Assignment 1', + studentName: 'Michael', + score: 80 +}, { + assignmentName: 'Assignment 2', + studentName: 'Nicholas', + score: 90 +}, { + assignmentName: 'Assignment 2', + studentName: 'Michael', + score: 100 +}, { + assignmentName: 'Assignment 3', + studentName: 'Nicholas', + score: 85 + }, { + assignmentName: 'Assignment 3', + studentName: 'Michael', + score: 65 +}] + +module.exports = { + grades +} \ No newline at end of file diff --git a/index.js b/index.js index 856a122..227f938 100644 --- a/index.js +++ b/index.js @@ -30,17 +30,25 @@ */ +const { grades } = require('./data/grades') + + + /** + + 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 */ - const gradeTotal = 0 - - +let gradeTotal = 0 +grades.forEach( (grade) => { + gradeTotal += grade.score +}) +console.log( {gradeTotal} ) /** Using reduce @@ -50,4 +58,16 @@ Replace `null` below with the use of `reduce` */ - gradeTotal = null + +// function totalGrade(grades) { +// return grades.reduce((acc, grade) => { return acc = acc + grade.score }, 0) +// } +// + +gradeTotal = grades.reduce( (total, grade) => { + console.log(total) + return total + grade.score + +}, 0 ) + +console.log ({gradeTotal})