diff --git a/data/grades.js b/data/grades.js new file mode 100644 index 0000000..fa2efff --- /dev/null +++ b/data/grades.js @@ -0,0 +1,32 @@ +module.exports = [ + { + assignmentName: 'Assignment 1', + studentName: 'Tito', + score: 50 + }, + { + assignmentName: 'Assignment 1', + studentName: 'Hollis', + score: 100 + }, + { + assignmentName: 'Assignment 2', + studentName: 'Tito', + score: 75 + }, + { + assignmentName: 'Assignment 2', + studentName: 'Hollis', + score: 80 + }, + { + assignmentName: 'Assignment 3', + studentName: 'Tito', + score: 95 + }, + { + assignmentName: 'Assignment 3', + studentName: 'Hollis', + score: 100 + } +] \ No newline at end of file diff --git a/index.js b/index.js index 856a122..d840bee 100644 --- a/index.js +++ b/index.js @@ -30,24 +30,47 @@ */ - /** +/** + + 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 grades = require('./data/grades') + +function forLoopAdd(array) { + let gradeTotal = 0 + array.forEach(grade => { + gradeTotal += grade.score + }) + return gradeTotal +} + +console.log(forLoopAdd(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 + +/** + + Using reduce + + 3) Use Array reduce to do the same total calculation logic + + Replace `null` below with the use of `reduce` */ - const gradeTotal = 0 +gradeTotal = grades.reduce((acc, grade) => acc + grade.score, 0) +console.log(gradeTotal) + +function recursionAdd([value, ...rest]){ + if(!rest.length){return value.score} - /** - - Using reduce + return value.score + recursionAdd(rest) +} - 3) Use Array reduce to do the same total calculation logic +console.log(recursionAdd(grades)) - Replace `null` below with the use of `reduce` - - */ - gradeTotal = null +let titoGrades = grades.filter(grade => grade.studentName === 'Tito').reduce((a, grade) => a+ grade.score, 0) +console.log(titoGrades) \ No newline at end of file