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

390
Views
Placing an if statement within a map or forEach function

I am building a small word chain turns-based game with Node.js, Express & Socket.io. The players and their scores are displayed, refreshed at every turn, through this function:

function updatePlayersList(players) {
  const players_list = document.getElementById('players_list');
  players_list.innerHTML = players.map(player => `<li id="${player.id}">${player.name} - ${player.score}</li>`).join('');
}

Here's the players array:

[
  {
    id: 'opYbiuXzExmymRPvAAAV',
    name: 'Paul',
    score: 0,
    room: 'room',
    playing: true
},
{
    id: 'uvs4wxpZwsmaQ3G4AAAX',
    name: 'Ezio',
    score: 0,
    room: 'room',
    playing: true
  }
]

Which works fine and displays this. The player's whose turn it is gets his name with a border. Here's a game with only two players but more can play.

In this idea, I want to strike the name of the players who lost during the game. Their name still gets displayed so everyone knows who's still in and who's out, but they can't play anymore obviously, just keep watching.

So I was thinking of an if statement inside the map function, adding a class .not-playing to the names of the players with playing: false (that's updated on the server side after each turn). But either way I am doing it wrong, or do I have to use something else? Here's the modified code, but then no more players get displayed, neither the playing nor the not playing ones. Not what I want, obviously.

players_list.innerHTML = players.map(player => {
    if (player.playing) {
      `<li id="${player.id}">${player.name} - ${player.score}</li>`
    } else {
      `<li id="${player.id}" class="not-playing">${player.name} - ${player.score}</li>`
    }
  }).join('');

I also tried with a forEach loop but got the same result.

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

0

In your modified code, you aren't returning anything from the map call. As long as you don't open curly braces, you implicitly return the value mapped to (this is some syntactic sugar JavaScript offers you), but once you open a block, you need to explicitly return from it:

players_list.innerHTML = players.map(player => {
    if (player.playing) {
        return `<li id="${player.id}">${player.name} - ${player.score}</li>`;
    } else {
        return `<li id="${player.id}" class="not-playing">${player.name} - ${player.score}</li>`;
    }
}).join('');
about 4 years ago · Juan Pablo Isaza Report

0

You should return something when doing map.

about 4 years ago · Juan Pablo Isaza Report

0

I'm not sure what your forEach looked like but I think I got the functionality you're looking for with this:

players.forEach(player => {
    if (player.playing) {
        players_list.innerHTML += `<li id="${player.id}">${player.name} - ${player.score}</li>`
    } else {
        players_list.innerHTML += `<li id="${player.id}" class="not-playing">${player.name} - ${player.score}</li>`
    }
 })
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!