skip to Main Content

I’m building the first part of the Rock-Paper-Scissors project of The Odin Project foundations course and my function playRound works but never stops running

function getComputerChoice(){
    //create array of (rock, paper, scissors)
    const listOfWeapons = new Array ('Rock','Paper','Scissors');
    //make computer choose randomly between the objects of the array
    let computerChoice = listOfWeapons[Math.floor(Math.random()*listOfWeapons.length)];
    console.log(computerChoice)
}
getComputerChoice();

function playRound(playerSelection, computerSelection){
    let getPlayerSelection = prompt('Choose your weapon!');
    if (getComputerChoice == getPlayerSelection){
        alert('It's a tie you both have choosen the same weapon')
    }
    for (getComputerChoice == 'Rock'; getPlayerSelection == 'Paper';){
        alert('You won paper beats rock')
    }
    for (getComputerChoice == 'Rock'; getPlayerSelection == 'Scissors';){
        alert('You lost rock beats scissors')
    }
    for (getComputerChoice == 'Paper'; getPlayerSelection == 'Scissors';){
        alert('You won scissors beats paper')
    }
    for (getComputerChoice == 'Paper'; getPlayerSelection == 'Rock';){
        alert('You lost paper beats rock')
    }
    for (getComputerChoice == 'Scissors'; getPlayerSelection == 'Rock';){
        alert('You won rock beats paper')
    }
    for (getComputerChoice == 'Scissors'; getPlayerSelection == 'Paper';){
        alert('You lost scissors beats paper')
    }
}
playRound();

How can I modify this code so the function terminates when it’s done? Or should I start over?

2

Answers


  1. You need to use if else logic not if followed by for. Replace each for with else if in your playRound function like this:

    function playRound(playerSelection, computerSelection){
        let getPlayerSelection = prompt('Choose your weapon!');
        if (getComputerChoice == getPlayerSelection){
            alert('It's a tie you both have choosen the same weapon')
        }
        else if (getComputerChoice == 'Rock' && getPlayerSelection == 'Paper';){
            alert('You won paper beats rock')
        }
        else if (getComputerChoice == 'Rock' && getPlayerSelection == 'Scissors';){
            alert('You lost rock beats scissors')
        }
        else if (getComputerChoice == 'Paper' && getPlayerSelection == 'Scissors';){
            alert('You won scissors beats paper')
        }
        else if (getComputerChoice == 'Paper' && getPlayerSelection == 'Rock';){
            alert('You lost paper beats rock')
        }
        else if (getComputerChoice == 'Scissors' && getPlayerSelection == 'Rock';){
            alert('You won rock beats paper')
        }
        else {
            alert('You lost scissors beats paper')
        }
    }
    
    Login or Signup to reply.
  2. There are multiple things wrong:

    1. You wanted to use if, but you used for
    2. You have to use and (&&) operator to compare many thing in one if statement
    3. getComputerChoice is a function, not a string
    4. getComputerChoice have return a value
    5. getComputerChoice have to be called inside playRound
    function getComputerChoice() {
      //create array of (rock, paper, scissors)
      const listOfWeapons = new Array('Rock', 'Paper', 'Scissors');
      //make computer choose randomly between the objects of the array
      let computerChoice = listOfWeapons[Math.floor(Math.random() * listOfWeapons.length)];
      console.log(computerChoice)
      return computerChoice
    }
    
    function playRound(playerSelection, computerSelection) {
      let getPlayerSelection = prompt('Choose your weapon!');
      const computerChoice = getComputerChoice()
      if (computerChoice == getPlayerSelection) {
        alert('It's a tie you both have choosen the same weapon')
      }
      if (computerChoice == 'Rock' && getPlayerSelection == 'Paper') {
        alert('You won paper beats rock')
      }
      if (computerChoice == 'Rock' && getPlayerSelection == 'Scissors') {
        alert('You lost rock beats scissors')
      }
      if (computerChoice == 'Paper' && getPlayerSelection == 'Scissors') {
        alert('You won scissors beats paper')
      }
      if (computerChoice == 'Paper' && getPlayerSelection == 'Rock') {
        alert('You lost paper beats rock')
      }
      if (computerChoice == 'Scissors' && getPlayerSelection == 'Rock') {
        alert('You won rock beats paper')
      }
      if (computerChoice == 'Scissors' && getPlayerSelection == 'Paper') {
        alert('You lost scissors beats paper')
      }
    }
    playRound();
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search