Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function countScores(people){
- let peopleObject = {};
- for (let person of people) {
- let { name, score } = person;
- // If the name is not in the peopleObject, initialize it with the score.
- // Otherwise, add the score to the existing value.
- if (!(name in peopleObject)) {
- peopleObject[name] = score;
- } else {
- peopleObject[name] += score;
- }
- }
- return peopleObject
- }
- // Example 1:
- const ppl = [
- { name: "Anthony", score: 10 },
- { name: "Fred", score : 10 },
- { name: "Anthony", score: -8 },
- { name: "Winnie", score: 12 }
- ];
- console.log(countScores(ppl))
Advertisement
Add Comment
Please, Sign In to add comment