diff --git a/data/grades.js b/data/grades.js new file mode 100644 index 0000000..50409a5 --- /dev/null +++ b/data/grades.js @@ -0,0 +1 @@ +const { grades } = require('./index.js') \ No newline at end of file diff --git a/index.js b/index.js index 856a122..7109af0 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,52 @@ 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 +*/ + +/* 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} ) */ - /** + +/** - 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 + */ +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 +} \ No newline at end of file