Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So I got help with this IMMEDIATELY from StackOverflow and apparently I've been accused of farming reputation points for finally figuring out the answers to some of my previous questions and then adding the answer in after it being posted here for hours. So, if someone wants to solve this roblem using the .reduce method instead of the .forEach method I'll mark you as the top answer/whatever.


Example:

let ppl = [{name: "Anthony", score: 10},
            {name: "Fred", score : 10},
            {name: "Anthony", score: -8},
            {name: "Winnie", score: 12}];

console.log(countScores(ppl)); //=> { Anthony: 2, Fred: 10, Winnie: 12 }



Sounds silly, but I am at a loss as to how to keep track of the scores through iterations here. I either end up getting NaN even when converting --->

score = (person[key])


when I add it to score in the Object it givs me some realy weird results.


+ expected - actual

 {
-  "Anthony": -8
+  "Anthony": 2
   "Fred": 10
   "Winnie": 12
 }


What I have tried:

function countScores(people) {
  // Your code here

  let scoreCount = {};
            

people.forEach(person => {  /// Go through the people
      let score = 0;   // Reset score placeholder to 0
      score += person.score;   /// Grab the score of person and set it to score
      name = person.name;      /// Grab the name of the person

    
    scoreCount[name] = score; // Push name/score to Object.

  })

    console.log(scoreCount)
    console.log("Endig Calculation of Scores....")
    return scoreCount;

};
Posted
Updated 6-Oct-22 22:23pm
v5

1 solution

Quote:
So I got help with this IMMEDIATELY from StackOverflow and apparently I've been accused of farming reputation points for finally figuring out the answers to some of my previous questions and then adding the answer in after it being posted here for hours. So, if someone wants to solve this roblem using the .reduce method instead of the .forEach method I'll mark you as the top answer/whatever.
Comments like this do not encourage people to help you. We are all volunteers here and help because we enjoy solving problems. But we do not take kindly to being given orders by people who mistakenly think we work for them.
 
Share this answer
 
Comments
Chris Aug2022 7-Oct-22 6:26am    
Straw Man Fallacy - A straw man fallacy occurs when someone takes another person’s argument or point, distorts it or exaggerates it in some kind of extreme way, and then attacks the extreme distortion, as if that is really the claim the first person is making.
Richard MacCutchan 7-Oct-22 7:47am    
There is no distortion, that is an exact copy of your question. And I have not attacked anything, merely pointed out that such comments do not help you to get help.
Chris Aug2022 7-Oct-22 8:33am    
Distortion: "being given orders by people who mistakenly think we work for them." --> Self Explainatory.
Distortion: "to help you" ---> I already got the answer and am not requesting help.

Again, the rules state: Provide an answer or move on to another question.
Richard MacCutchan 7-Oct-22 8:51am    
lol.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900