skip to Main Content
function playRound(playerSelection, computerSelection) {
   
    if (playerSelection == computerSelection) {
        console.log("its a tie")

       } else if (playerSelection == "rock" && computerSelection == "paper") {
        return "you lose" 

     } else if (playerSelection == "rock" && computerSelection == "scissors") {
        
        return "you win"  

     }  else if (playerSelection == "paper" && computerSelection == "scissors") {
        return "you lose" 

     } else if (playerSelection == "paper" && computerSelection == "rock") {
        return "you win" 

     } else if (playerSelection == "scissors" && computerSelection == "rock") {
        return "you lose" 

     } else if (playerSelection == "scissors" && computerSelection == "paper") {
        return "you win" 
     }    
}
   
let playerSelection = parseInt(prompt("input rock paper or scissors"))
let computerSelection = Math.random();

if (computerSelection < 0.34){
  computerSelection = "rock"
} else if(computerSelection <=0.67){
  computerSelection = "paper"
} else {
  computerSelection = "scissors"
}

console.log(playRound(playerSelection, computerSelection))



  

I know its probably not the best method but i thought it should get the job done, any help is useful as i don’t know where the mistake can be, thank you. I looked at some other posts but couldn’t find the solution. This is my first post so if there is some info missing or something just let me know!

2

Answers


  1. There are a few issues,

    1. not returning when it’s tied.
    2. converting the prompt into an int.

    Try the following

    function playRound(playerSelection, computerSelection) {
      if (playerSelection === computerSelection) {
        return 'its a tie';
      } else if (playerSelection === 'rock' && computerSelection === 'paper') {
        return 'you lose';
      } else if (playerSelection === 'rock' && computerSelection === 'scissors') {
        return 'you win';
      } else if (playerSelection === 'paper' && computerSelection === 'scissors') {
        return 'you lose';
      } else if (playerSelection === 'paper' && computerSelection === 'rock') {
        return 'you win';
      } else if (playerSelection === 'scissors' && computerSelection === 'rock') {
        return 'you lose';
      } else if (playerSelection === 'scissors' && computerSelection === 'paper') {
        return 'you win';
      }
    }
    
    let playerSelection = prompt(`Type 'rock' or 'paper' or 'scissors'`)
    
    let computerSelection = Math.random();
    if (computerSelection < 0.34) {
      computerSelection = 'rock';
    } else if (computerSelection <= 0.67) { 
      computerSelection = 'paper';
    } else {
      computerSelection = 'scissors';
    }
    
    console.log(playRound(playerSelection, computerSelection));
    
    
    Login or Signup to reply.
  2. You shouldn’t be using parseInt because the user will be entering string data, not numbers.

    You also need to move some of your console.log() statements around a bit, but other than that, you’re good to go!

    // No parseInt when the input is strings that need to be strings
    let playerSelection = prompt("input rock paper or scissors");
    let computerSelection = Math.random();
    
    if (computerSelection < 0.34) {
      computerSelection = "rock"
    } else if(computerSelection <=0.67) {
      computerSelection = "paper"
    } else {
      computerSelection = "scissors"
    }
    
    playRound(playerSelection, computerSelection)
    
    function playRound(playerSelection, computerSelection) {
        // Instead of returning in each branch, just set the value of a string that you can
        // report at the end of the function.
        let result = "";
       
        if (playerSelection == computerSelection) {
           result = "its a tie"
         } else if (playerSelection == "rock" && computerSelection == "paper") {
            result = "you lose" 
         } else if (playerSelection == "rock" && computerSelection == "scissors") {
            result =  "you win"  
         }  else if (playerSelection == "paper" && computerSelection == "scissors") {
            result =  "you lose" 
         } else if (playerSelection == "paper" && computerSelection == "rock") {
            result =  "you win" 
         } else if (playerSelection == "scissors" && computerSelection == "rock") {
            result =  "you lose" 
         } else if (playerSelection == "scissors" && computerSelection == "paper") {
            reresult = "you win" 
         }
         
         console.log("Compupter picked: " + computerSelection, "You picked " + playerSelection, result);
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search