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

220
Visualizações
Javascript conditional not alerting when its condition is reached

I made a for loop in game() function to have the user input rock, paper, or scissors. If the game is played 5 times the loop is supposed to alert "You played 5 games already" but it doesn't alert when the condition is met. I tried adding return before alert, I also tried using >= instead of === but it doesn't work.

function game(){
    for(let i = 0; i < 5; i++){
        if(i < 5){
            let getSelect = prompt("Choose Rock, Paper, or Scissors", "");
            let getWinOrLose = alert(playRound(getSelect, computerPlay()));
                if( i === 5 ){
                    alert("You played 5 games already");
                }
        }
    }
}

console.log(game());

Check the code above ^

And here is the fiddle: https://jsfiddle.net/JaredDev/cbmLetuz/2/

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

It won´t show the alert because condition i===5 is never reached since it is guarded by i<5. Plus the loop never reaches value of 5.

about 4 years ago · Juan Pablo Isaza Relatório

0

Try for (let i = 0; i <= 5; i++) { the loop will run until i is less or equal to 5 and should trigger the i === 5 condition.

Just be aware that the loop will run 6 times for the i values 0, 1, 2, 3, 4, 5 so you may want to consider tweaking the numbers to your specs.

about 4 years ago · Juan Pablo Isaza Relatório

0

It is not i===5. Since, It is i < 5, The condition satisfies during i===4 itself. So change i===5 to i===4. As like the code given below,

function game(){
    for(let i = 0; i < 5; i++){
        if(i < 5){
            let getSelect = prompt("Choose Rock, Paper, or Scissors", "");
            let getWinOrLose = alert(playRound(getSelect, computerPlay()));
                if( i === 4 ){
                    alert("You played 5 games already");
                }
        }
    }
}

console.log(game());
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