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
34 changes: 34 additions & 0 deletions data/grades.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
grades = [{
assignmentName: 'Assignment01',
studentName: 'Timmy',
score: 90
},
{
assignmentName: 'Assignment01',
studentName: 'Zoey',
score: 95
},
{
assignmentName: 'Assignment02',
studentName: 'Timmy',
score: 85
},
{
assignmentName: 'Assignment02',
studentName: 'Zoey',
score: 70
},
{
assignmentName: 'Assignment03',
studentName: 'Zoey',
score: 100
},
{
assignmentName: 'Assignment03',
studentName: 'Timmy',
score: 60
}]

module.exports = {
grades
}
71 changes: 59 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,72 @@
- Use 2 distinct student names

*/
const { grades } = require('./data/grades')

/**
// console.log({ grades })
/**

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
*/
// expect gradeTotal = 500
function totalGrades(grades) {
let gradeTotal = 0
grades.forEach((grade) => {
const { score } = grade
gradeTotal += score
})
return gradeTotal
}

console.log(totalGrades(grades))

// add in for...in loop option 1
gradeTotal = 0

for (const index in grades) {
const { score } = grades[index]
console.log({ score })
gradeTotal += score
}
console.log({ gradeTotal })

gradeTotal = 0

/**
for (const index in grades) {
const grade = grades[index]
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
*/
function totalGrades(grades) {
const gradeTotal = grades.reduce((total, grade) => {
console.log(total, grade)
return total + grade.score
}, 0)
return gradeTotal
}

console.log(totalGrades(grades))

function totalGradesAlt(grades) {
const gradeTotal = grades.reduce((total, grade) => {
console.log(total, grade)
const { score } = grade
return total + score
}, 0)
return gradeTotal
}

console.log(totalGradesAlt(grades))