Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

395
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

0

You should return something when doing map.

about 4 years ago · Juan Pablo Isaza Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda