For loop isn't working properly


function playRound(playerSelection) {
    for(let i = 0; i < 3; i++) {
   computerSelection = getComputerChoice();

   if (playerSelection === 'Rock' && computerSelection == "paper") {
    div.innerText = "";
        computerScore++;
        div.innerText += "You Lose! Paper beats Rock";
        
        
    }
    else if (playerSelection === 'Paper' && computerSelection == "rock") {
        div.innerText = "";
        yourScore++;
        return div.innerText += "You Win! Paper beats Rock";
        
    }
    else if (playerSelection === "Scissor" && computerSelection == "rock") {
        div.innerText = "";
        computerScore++;
        return div.innerText += "You Lose! Rock beats Scissor";
        
    }
    else if (playerSelection === "Rock" && computerSelection == "scissor") {
        div.innerText = "";
        yourScore++;
        return div.innerText += "You Win! Rock beats Scissor";
    
    }
    else if (playerSelection === "Scissor" && computerSelection == "paper") {
        div.innerText = "";
        yourScore++;
        return div.innerText += "You Win! Scissor beats Paper";
        
    }
    else if (playerSelection === "Paper" && computerSelection == "scissor") {
        div.innerText = "";
        computerScore++;
        return div.innerText += "You Lose! Scissor beats Paper";
        
    }
    else if (playerSelection == computerSelection) {
        div.innerText = "";
        return div.innerText += "Tie! Try again";
        
    }
    
     }
    if (computerScore > yourScore) {
        div.innerText = "";
       return div.innerText += "You lose! try again!";
    } else {
        div.innerText = "";
        return div.innerText += "Congratulations! You win!";
    }
   
}

In what way isn’t the for loop working?

For one thing, you seem to have an extra closing curly brace (}) in there.

Next, I would clean things up a little. For one, if you are going to return, then you don’t need the else. Next, to be clear return will exit the current function. It doesn’t skip to the next iteration - that would be continue. If that’s what you want, then you want a structure like:

for (let i = 0; i < 3; i++) {
  if (playerSelection === 'Rock' && computerSelection == "paper") {
    div.innerText = "";
    computerScore++;
    div.innerText += "You Lose! Paper beats Rock";
    continue;
  }
  if (playerSelection === 'Paper' && computerSelection == "rock") {
    div.innerText = "";
    yourScore++;
    div.innerText += "You Win! Paper beats Rock";
    continue;
  }
  // ...

or something like that. There are other ways to simplify it, but that is a step in the right direction.

I’m trying to make stone paper scissor game

let yourScore = 0;
let computerScore = 0; 
var button = document.querySelector('.btn');
let div = document.getElementById('result');




function getComputerChoice() {
    let choices = [
        "rock",
        "paper",
        "scissor"
    ]
    let compChoice = Math.floor(Math.random() * 3);
    return choices[compChoice];
   
}

function playRound(playerSelection) {
    for(let i = 0; i < 3; i++) {
   computerSelection = getComputerChoice();

   if (playerSelection === 'Rock' && computerSelection == "paper") {
    div.innerText = "";
        computerScore++;
        div.innerText += "You Lose! Paper beats Rock";
        
        
    }
    else if (playerSelection === 'Paper' && computerSelection == "rock") {
        div.innerText = "";
        yourScore++;
        return div.innerText += "You Win! Paper beats Rock";
        
    }
    else if (playerSelection === "Scissor" && computerSelection == "rock") {
        div.innerText = "";
        computerScore++;
        return div.innerText += "You Lose! Rock beats Scissor";
        
    }
    else if (playerSelection === "Rock" && computerSelection == "scissor") {
        div.innerText = "";
        yourScore++;
        return div.innerText += "You Win! Rock beats Scissor";
    
    }
    else if (playerSelection === "Scissor" && computerSelection == "paper") {
        div.innerText = "";
        yourScore++;
        return div.innerText += "You Win! Scissor beats Paper";
        
    }
    else if (playerSelection === "Paper" && computerSelection == "scissor") {
        div.innerText = "";
        computerScore++;
        return div.innerText += "You Lose! Scissor beats Paper";
        
    }
    else if (playerSelection == computerSelection) {
        div.innerText = "";
        return div.innerText += "Tie! Try again";
        
    }
    
     }
    if (computerScore > yourScore) {
        div.innerText = "";
       return div.innerText += "You lose! try again!";
    } else {
        div.innerText = "";
        return div.innerText += "Congratulations! You win!";
    }
   
}

  


function init() {
    document.querySelector(".buttons").addEventListener('click', function(event) {
        playRound(event.target.innerText);
        
      
    });
 
}
init();

sorry…its a for loop inside function. i have edited the code

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.