Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

When I run my code in the console I keep getting the same "tie" output instead of win or lose

my computerPlay function works and gives me random rock, paper and scissors each time, my userChoice function works as well. I am having trouble getting my play round function to tell me whether I win lose or get a tie. It constantly just gives me Tie. I’m lost on where I’ve gone wrong.

// computerPlay function has the computer pick a random number 0-2 and assigns it to rock paper or scissors
function computerPlay() {
    const randomNumber = Math.floor(Math.random() * 3);
      if (randomNumber === 0){
        console.log("Computer played: rock");
        //return "rock";
    } else if (randomNumber === 1){
        console.log("Computer played: paper");
       // return "paper";
    } else if (randomNumber === 2){
        console.log("Computer played: scissors")
       // return "scissors";
    }
   
}
 
//userChoice function lets the player enter rock paper or scissors

function userChoice(){
    let user = prompt("Enter your choice ");
    user = user.toLowerCase(); 
    if (user === "rock" || user === "paper" || user === "scissors"){
        console.log("Your choice is: " + user);
    } else {
        console.log("invalid choice");
    }
    //console.log("Your choice is: " + user);  
}

//userChoice();
//computerPlay();
//console.log("Computer played: " + computerPlay());



let playerSelection = userChoice();

let computerSelection = computerPlay();

// playRound function tells the program the rules behind winning and losing
function playRound(){
    if (playerSelection === computerSelection){
        console.log("Tie");
    } else if (playerSelection === "rock" && computerSelection === 1){
        console.log("You lose");
    } else if (playerSelection === "paper" && computerSelection === 2){
        console.log("You lose");
    } else if (playerSelection === "scissors" && computerSelection === 0){
        console.log("You lose");
    } else if (playerSelection === "rock" && computerSelection === 2){
        console.log("You win");
    } else if (playerSelection === "paper" && computerSelection === 0){
        console.log("You win");
    } else if (playerSelection === "scissors" && computerSelection === 1){
        console.log("You win");
    }    
}

playRound();
//console.log(playRound(playerSelection, computerSelection));

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Try this by using return statement to your function.

function computerPlay() {
    const randomNumber = Math.floor(Math.random() * 3);
      if (randomNumber === 0){
        console.log("Computer played: rock");
        return "rock";
    } else if (randomNumber === 1){
        console.log("Computer played: paper");
       return "paper";
    } else if (randomNumber === 2){
        console.log("Computer played: scissors")
       return "scissors";
    }
   
}
 
//userChoice function lets the player enter rock paper or scissors

function userChoice(){
    let user = prompt("Enter your choice ");
    user = user.toLowerCase(); 
    if (user === "rock" || user === "paper" || user === "scissors"){
        console.log("Your choice is: " + user);
    } else {
        console.log("invalid choice");
    }
    return user
    //console.log("Your choice is: " + user);  
}

//userChoice();
//computerPlay();
//console.log("Computer played: " + computerPlay());



let playerSelection = userChoice();

let computerSelection = computerPlay();
console.log( playerSelection)
console.log(computerSelection)

// playRound function tells the program the rules behind winning and losing
function playRound(){
    if (playerSelection === computerSelection){
        console.log("Tie");
    } else if (playerSelection === "rock" && computerSelection === "paper"){
        console.log("You lose");
    } else if (playerSelection === "paper" && computerSelection === "scissors"){
        console.log("You lose");
    } else if (playerSelection === "scissors" && computerSelection === "rock"){
        console.log("You lose");
    } else if (playerSelection === "rock" && computerSelection === "scissors"){
        console.log("You win");
    } else if (playerSelection === "paper" && computerSelection === "rock"){
        console.log("You win");
    } else if (playerSelection === "scissors" && computerSelection === "paper"){
        console.log("You win");
    }    
}

playRound();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading