Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions data/grades.js
Original file line number Diff line number Diff line change
@@ -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
}
]
51 changes: 37 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)