A little help concerning 9/9 Next Steps!


#1


Hello,
I am trying to add solutions for inappropriate choices and asking for new ones if the result was a tie. In fact the code works perfectly concerns the game but the problem is that it does not show the new result after asking the user to try again.

I will be grateful for any help :slight_smile:


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 {
	computerChoice = "scissors";
} console.log("Computer: " + computerChoice);
function compare(choice1,choice2){
    
    if(choice1===choice2){
        console.log("The result is a tie!");
        confirm("Choose again");  //Asking for new choices
        var userChoice = prompt("Do you choose rock, paper or scissors?"); 
        if(choice1 === "rock"){
        if(choice2 === "scissors"){
            return "rock wins";
        }else{
            return 'paper wins';
        }
        }
        else if(choice1 === "paper"){
        if(choice2 === "rock"){
            return 'paper wins';
        }else{
            return 'scissors wins';
        }
        }
    else if(choice1 === "scissors"){
        if(choice2 === "rock"){
            return 'rock wins';
        }else{
            return 'scissors wins';
        }
    }

    

    }else if(choice1 === "rock"){ //Game
        if(choice2 === "scissors"){
            return "rock wins";
        }else{
            return 'paper wins';
        }
    }else if(choice1 === "paper"){
        if(choice2 === "rock"){
            return 'paper wins';
        }else{
            return 'scissors wins';
        }
    }else if(choice1 === "scissors"){
        if(choice2 === "rock"){
            return 'rock wins';
        }else{
            return 'scissors wins';
        }
    }
    else if(choice1 != "rock"){  // inappropriate choice
        if(choice1 != "paper"){
            if(choice1 != "scissors"){
                alert("Wrong choice!, try again");
                var userChoice = prompt("Do you choose rock, paper or scissors?"); 
        if(choice1 === "rock"){
        if(choice2 === "scissors"){
            return "rock wins";
        }else{
            return 'paper wins';
        }
        }
        else if(choice1 === "paper"){
        if(choice2 === "rock"){
            return 'paper wins';
        }else{
            return 'scissors wins';
        }
        }
    else if(choice1 === "scissors"){
        if(choice2 === "rock"){
            return 'rock wins';
        }else{
            return 'scissors wins';
        }
    }
            }
        }
    }
};
compare(userChoice, computerChoice);


Next Steps
#2

okay, but what if the user enters a invalid input twice?

the problem you experienced is because of function parameters/arguments:

function compare(choice1,choice2){
  // function body
}
compare(userChoice, computerChoice);

inside your function you overwrite userChoice, but inside the function, you compare with choice1 and choice2, not userChoice.


#3

Thank you for your answer. It is quite helpful. I removed the inappropriate choices from the function and made it a separate part below the function. Like this

if(userChoice != "rock"){ // inappropriate choice
if(userChoice != "paper"){
if(userChoice != "scissors"){
alert("Wrong choice!, try again");
var userChoice = prompt("Do you choose rock, paper or scissors?");
}
}
}
I've used while and do-while loops trying to stuck with the user till inputing an appropriate answer. :smiley: But I have suffered from crashes every time I run the game!


#4

but a do while loop should be pretty straight forward?

do {
    var userChoice = prompt("Do you choose rock, paper or scissors?"); 
} while(userChoice != "rock" && userChoice != "paper" && userChoice != "scissors")

#5

Exactly!
I have used the wrong logical operator. I think it is a good lesson to understand the importance of using the appropriate logical one :slight_smile:
I am grateful for you. Thank you.

do{
if(userChoice != "rock"){ // inappropriate choice
if(userChoice != "paper"){
if(userChoice != "scissors"){
alert("Wrong choice!, try again");
var userChoice = prompt("Do you choose rock, paper or scissors?");
}
}
}
}while(userChoice!= "rock" || userChoice != "paper" || userChoice != "scissors" );

Edit: It is the semi-colon at the end of while() loop that caused the crash :smiley:


#6

do i need to explain something? I don't see why you have both if and while, if you have while condition, you no longer need if


#7

Oh, It is my own fault. I express my sorrow.


#8

i was just making sure you understood everything, and asking if you wanted explanation of something? I should have formulated it differently


#9

Thank you very much, It was exactly what I'm looking for. But, how can I use some code to accept rock, ROCK, Rock, RoCk and other variations as a correct answer? @stetim94


#10

why not simply use toLowerCase()? this function is covered if i am not mistaken, you can simply apply it.


#11

The system shows me an error, but it's bcause the answer is different as it expect. But I've used like this:

compare(userChoice.toLowerCase(), computerChoice);

is that ok? @stetim94


#12

here you do the validation of valid user input:

do {
    var userChoice = prompt("Do you choose rock, paper or scissors?"); 
} while(userChoice != "rock" && userChoice != "paper" && userChoice != "scissors")

wouldn't it be logic/useful to apply the case conversion here? Since the loop will validate your input


#13

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