9. Next Steps Rock Paper Scissors ROPE / TIE

games
game

#1


https://www.codecademy.com/en/courses/javascript-beginner-en-Bthev-mskY8/1/5?curriculum_id=506324b3a7dffd00020bf661


I got help previously and the code I pasted in from here worked, so I typed it in myself, now it won't work. I have read through it so many times I cannot see why it isn't working: https://www.codecademy.com/en/forum_questions/50912c168977000200004007


var playGame = function () {

var userChoice = prompt("Do you choose rock, paper, scissors or rope?");
var computerChoice = Math.random();
if (computerChoice <= 0.25) {
	computerChoice = "rock";
} else if (computerChoice <= 0.50) {
	computerChoice = "paper";
} else if (computerChoice <= 0.75) {
	computerChoice = "scissors";
} else {
    computerChoice = "rope";
}



var compare = function (choice1, choice2) {
    if (choice1 === choice2) {
        return (0);
    } else if (choice1 === "rock") {
        if (choice2 ==="scissors") {
            return ("rock wins");
        } else if (choice2 === "paper") { 
            return ("paper wins");
        } else if (choice2 === "rope") {
            return ("rope wins");
        }
        
    } else if (choice1 === "paper") {
        if (choice2 === "rock") {
            return ("paper wins");
        } else if (choice2 === "scissors') {
            return ("scissors wins");
        } else if (choice2 === "rope") {
            return ("rope wins");
        }
        
    } else if (choice1 === "scissors") {
        if (choice2 === "rock") {
            return ("rock wins");
        } else if (choice2 === "paper") {
            return ("scissors wins");
        } else if (choice2 === "rope") {
            return ("rope wins");
        }
        
    } else if (choice1 === "rope") {
        if (choice2 === "rock") {
            return ("rope wins");
        } else if (choice2 ==="paper") {
            return ("rope wins");
        } else if (choice2 === "scissors") {
            return ("rope wins");
        }
            
    }
    return ("That is not a valid choice!!!");

};

var result = compare (userChoice,computerChoice);
if (result === 0) {
    playGame();
} else {
    console.log(result);
}
}

playGame();


#2

else if (choice2 === "scissors') // have a close look here :slight_smile:


#3

"scissors' - Amazing! I was looking at it for so long, I just couldn't see it! Thank you so much! It's just the invalid command that's not working now.


#4

invalid command!!!! what is that??????


#5

Is this the code block for an else if or an else statement?


#6

When you select a choice that isn't rock paper scissors rope. It doesn't start the game again. I wanted it to say "That isn't a valid choice" and then the prompt box to play again appear?


#7

"That isn't a valid choice" displays, but the game doesn't restart...


#8

so simple :slight_smile:
just add this

if (result === 0 || result==="That is not a valid choice!!!" ) {
playGame();


#9

Thanks again. Just need to remember, "think logically!"


#10

yap :slight_smile: try as much as you can :slight_smile:


#11

can someone tell me the problem ? (SyntaxError: Unexpected end of input)

var playGame = function() {
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
}
else if(computerChoice <= 0.67) {
computerChoice = "paper";
}
else if (computerChoice <= 0.75) {
computerChoice = "scissors";
}
else {
computerChoice = "rope";
}
console.log("Computer: " + computerChoice);

var compare = function (choice1,choice2) {
if (choice1 === choice2) {
return (0);
}
else if (choice1 === "rock") {
if (choice2 === "scissors") {
return ("rock wins");
}
else if (choice2 === "paper" ) {
return ("ppaer wins");
}
else if (choice2 === "rope" ) {
return ("rope wins");
}
}
else if (choice1 === "paper" ) {
if (choice2 === "rock"){
return ("rock wins");
}
else if(choice2 === "scissors") {
return ("scissors wins");
}
else if (choice2 === "rope" ) {
return ("rope wins");
}
}
else if(choice1 === "scissors") {
if(choice2 === "rock"){
return ("rock wins");
}
else if(choice2 === "paper" ) {
return ("scissors wins");
}
else if ( choice2 === "rope") {
return ("rope wins");
}

else {
        return ("rope wins");
    }
}
return ("This is not a valid choice!!!");

};
var result = compare (userChoice,computerChoice);
if (result === 0 || result==="This is not a valid choice!!!") {
playgame();
}
else {
console.log(result);
}
playgame();


#12

One thing you could add to fix that is add a curly at the end of your compare function


#13

here after

else {
console.log(result);
}
//here close the playGame function

and

if (result === 0 || result==="This is not a valid choice!!!") {
playgame();  // here write playGame not playgame

and 
again
playgame();  // here playGame

#14

Hii.

My code works fine but there is a small error, please help me to figure it out.when userChoice is different from computerChoice , for example userChoice=paper and computerChoice=scissors it displays "scissors wins".But when userChoice=computerChoice foe example both choose rope then it displays "its a tie" & "choose other options".when i choose option paper (userChoice) and computerChoice is scissors it displays "scissors wins" which is as expected and also displays undefined which is unexpected.why is showing undefined??

var playGame=function(){
var userChoice = prompt("Do you choose rock, paper , scissors or rope ?");
var computerChoice = Math.random();
if (computerChoice < 0.34) 
{
	computerChoice = "rock";
} 
else if(computerChoice <= 0.67) 
{
	computerChoice = "paper";
} 
else if(computerChoice <= 0.75)
{
	computerChoice = "scissors";
} 
else
{
    computerChoice = "rope";
}
console.log("Computer: " + computerChoice);


var compare = function(choice1,choice2)
{
    if(choice1===choice2)
    {
        confirm( "The result is a tie!");
        confirm("Do you want to enter other choices");
        playGame();
        
    }
    else if(choice1==="rock")
    {
        if(choice2==="scissors")
        {
            return "rock wins!";
        }
        else if(choice2==="paper")
        {
            return "paper wins!";
        }
        else
        {
            return "rope wins!";
        }
    }
    else if(choice1==="paper")
    {
        if(choice2==="rock")
        {
            return "paper wins!";
        }
        else if(choice2==="scissors")
        {
            return "scissors wins!";
        }
        else
        {
            return "rope wins!";
        }
    }
    else if(choice1==="scissors")
    {
        if(choice2==="rock")
        {
            return "rock wins!";
        }
        else if(choice2==="paper")
        {
            return "scissors wins!";
        }
        else
        {
            return "rope wins!";
        }
    }
    else if(choice1==="rope")
    {
        if(choice2==="rock")
        {
            return "rope wins!";
        }
        else if(choice2==="paper")
        {
            return "rope wins!";
        }
        else
        {
            return "scissors wins!";
        }

    }  
    else
        {
            return "Your choice is invalid"
        }
}
    console.log(compare(userChoice,computerChoice));
}
    playGame();

#15

Hiii

I have figured it out..now it works fine. :slight_smile:


#16

Thanks bro :slight_smile: it worked


#17

Thanks bro :slight_smile: it works


#18

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