c-mcbride

Working in POJOs

Jan 28th, 2024
972
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function countScores(people){
  2.     let peopleObject = {};
  3.  
  4.     for (let person of people) {
  5.         let { name, score } = person;
  6.  
  7.         // If the name is not in the peopleObject, initialize it with the score.
  8.         // Otherwise, add the score to the existing value.
  9.         if (!(name in peopleObject)) {
  10.             peopleObject[name] = score;
  11.         } else {
  12.             peopleObject[name] += score;
  13.         }
  14.     }
  15.  
  16.     return peopleObject
  17. }
  18.  
  19. // Example 1:
  20. const ppl = [
  21.   { name: "Anthony", score: 10 },
  22.   { name: "Fred", score : 10 },
  23.   { name: "Anthony", score: -8 },
  24.   { name: "Winnie", score: 12 }
  25. ];
  26.  
  27. console.log(countScores(ppl))
Advertisement
Add Comment
Please, Sign In to add comment