diff --git a/data/grades.js b/data/grades.js new file mode 100644 index 0000000..b225195 --- /dev/null +++ b/data/grades.js @@ -0,0 +1,40 @@ +let grades = [{ + assignmentName: 'Assignment01', + studentName: 'Seven', + score: 86 +}, { + assignmentName: 'Assignment02', + studentName: 'Geneva', + score: 71 +}, { + assignmentName: 'Assignment03', + studentName: 'Seven', + score: 90 +}, { + assignmentName: 'Assignment04', + studentName: 'Geneva', + score: 99 +}, { + assignmentName: 'Assignment05', + studentName: 'Seven', + score: 62 +}, { + assignmentName: 'Assignment06', + studentName: 'Geneva', + score: 87 +}]; + + + + + + + + + + + + +module.exports = { + grades +} \ No newline at end of file diff --git a/index.js b/index.js index 856a122..a786e23 100644 --- a/index.js +++ b/index.js @@ -30,24 +30,68 @@ */ - /** +/** - 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 { grades } = require('./data/grades') + +/* +let gradeTotal = 0 + +for (let i = 0; i < grades.length; i++) { + gradeTotal = grades.score += grades[i] +} + + +console.log(grades, gradeTotal) + +*/ + +// for In +/* +for (const index in grades) { + const { score } = grades[index] + console.log({ score }) + gradeTotal += score +} +*/ - */ - const gradeTotal = 0 - /** - - Using reduce - 3) Use Array reduce to do the same total calculation logic +// forEach +/* +let gradeTotal = 0; - Replace `null` below with the use of `reduce` +gradeTotal.forEach(grade => grades.score += grades.score) +*/ +/* +gradeTotal = 0 +grades.forEach(grade => { + gradeTotal += grade.score +}) +*/ + +// + +/** - */ - gradeTotal = null + Using reduce + 3) Use Array reduce to do the same total calculation logic + Replace `null` below with the use of `reduce` + + */ + + + +gradeTotal = grades.reduce((total, grade) => { + console.log(total, grade) + return total + grade.score +}, 0) + +console.log(gradeTotal) \ No newline at end of file