Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
Modify an element in forEach doubles the entries

I'm currently setting up this js exercise here however it seems it's not working corretcly as I got the duplication of all the entries, I cannot understand why.

Here's the code:

export function updateScore(scoreBoard, player, points) {
  scoreBoard[player] = scoreBoard[player] + points;
  return scoreBoard;
}

export function applyMondayBonus(scoreBoard) {
  Object.keys(scoreBoard).forEach((element) =>{
  updateScore(scoreBoard,element,scoreBoard[element] + 100)
  } );
  return scoreBoard;
}

Once I run the code: I got this output

Object {
-   "Amil Pastorius": 445,
-   "Jesse Johnson": 222,
-   "Min-seo Shin": 119,
+   "Amil Pastorius": 790,
+   "Jesse Johnson": 344,
+   "Min-seo Shin": 138,
  }

Instead of this

{
      'Amil Pastorius': 445,
      'Min-seo Shin': 119,
      'Jesse Johnson': 222,
    };

Thanks in advance

EDIT: My bad, that was a logical error as @Ivar said, I'm passing scoreBoard[element] + 100 as a parameter, then again use scoreBoard[element] in scoreBoard[player] + points.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could very simply write:

export function applyMondayBonus(scoreBoard = {}){
   Object.keys(scoreBoard).forEach(player => {
      scoreBoard[player] += 100
   })
   return scoreBoard
}

My advice would be to just write the function logic directly in the forEach loop. For such small and direct logic, a separate function could be a hassle.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!