Rock, paper, scissors. Inappropriate choices


#1


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


I'm trying to make the system ask the user to choose again in the case of an inappropriate choice. But I can't make the system compare the choices again.


var userChoice;
var computerChoice;

var choose = function () {
    userChoice = prompt("Do you choose rock, paper or scissors?");
    console.log ("User: " + userChoice);

    computerChoice = Math.random();
    if (computerChoice < 0.34) {
	    computerChoice = "rock";
    } else if(computerChoice <= 0.67) {
	    computerChoice = "paper";
    } else {
    	computerChoice = "scissors";
    } 
    console.log("Computer: " + computerChoice);  
}

var compare = function(choice1, choice2) {
    if(choice1 === choice2) {
        return ("The result is a tie!");
    } else 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 {
        confirm ("You have to choose among the 3 options!");
        choose ();
        compare (userChoice, computerChoice);
    }
}

choose ();
compare (userChoice, computerChoice);


#2

call compare from inside the choose function


#3

Do you mean like this? It's not working.

var userChoice;
var computerChoice;

var compare = function(choice1, choice2) {
    if(choice1 === choice2) {
        return ("The result is a tie!");
    } else 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 {
        confirm ("You have to choose among the 3 options!");
        choose ();
    }
}

var choose = function () {
    userChoice = prompt("Do you choose rock, paper or scissors?");
    console.log ("User: " + userChoice);

    computerChoice = Math.random();
    if (computerChoice < 0.34) {
	    computerChoice = "rock";
    } else if(computerChoice <= 0.67) {
	    computerChoice = "paper";
    } else {
    	computerChoice = "scissors";
    } 
    console.log("Computer: " + computerChoice);  
    compare (userChoice, computerChoice);
}

choose ();

#4

yea, this is what i mean. This way, compare is called again when choose is called (which happens when the user makes an inappropriate choice)

Are you happy with this result?


#5

Well, I thought it would work, but it isn't. It only compares the first time.


#6

its comparing, the result of this comparison is simply not shown to the console.

The problem is that the function calls inside the function don't return anything, they should return:

return compare (userChoice, computerChoice);

otherwise the value won't get to the first function on the last line of your code


#7

I've changed my code to your suggestion:

var userChoice;
var computerChoice;

var compare = function(choice1, choice2) {
    if(choice1 === choice2) {
        return ("The result is a tie!");
    } else 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 {
        console.log ("You have to choose among the 3 options!");
        choose ();
    }
}

var choose = function () {
    userChoice = prompt("Do you choose rock, paper or scissors?");
    console.log ("User: " + userChoice);

    computerChoice = Math.random();
    if (computerChoice < 0.34) {
	    computerChoice = "rock";
    } else if(computerChoice <= 0.67) {
	    computerChoice = "paper";
    } else {
    	computerChoice = "scissors";
    } 
    console.log ("Computer: " + computerChoice);  
    return compare (userChoice, computerChoice);
}

choose ();

But it still doesn't work. It doesn't show the last comparison and this is what appears on the console:

User: test
Computer: rock
You have to choose among the 3 options!
User: test2
Computer: paper
You have to choose among the 3 options!
User: paper
Computer: rock


#8

why the return compare there ?? that isn't right is it ?

i believe i done it with a console.log(compare(userChoice, computerChoice)) where you have the choose (); not shure but you can try :wink:


#9

It works when I try this, but the console shows "undefined" at the end.


#10

i'm trying to help, but i seem stuck on the same thing too :see_no_evil: , my code is diffrent than yours...


#11

Could you post your code here so we can compare?


#12

Yeah shure, but i haven't included the (you can only chose 1 of the 3) option yet :see_no_evil:

Hint: put your own code in a text file if you are going to post mine in your browser

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

var compare = function(choice1,choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!"
    }
    else 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"
        }
    }
}
console.log(compare(userChoice, computerChoice))

#13

@dkdemen, cleite17 is trying to build an extension to the game, did you get this? Your code like it should be at the end of the lesson


#14

i demonstrated one, but the other function call in the function:

else {
        console.log ("You have to choose among the 3 options!");
        choose ();
    }

has obviously the same problem.


#15

Yeah i realised it, after that i was doing the same to help Cleite17, send him a message with how far i got with helping him do it,

i think i get what he means but the console still spits out more than 2 outcomes...

is it possible to do a ifstatement inside a else or else if ??
i tried to do " confirm("You have to choose among the 3 options!"); "
but can't controll the true false outcome...


#16

Cleite17 seem to have progressed further already, and is pretty close to solving it.

Huge part of your compare function is nested if statement inside else if statements, so i can't take that question very seriously.

you can simply store the result of confirm in a variable:

x = confirm("You have to choose among the 3 options!");

#17

yeah i just saw what i typed, i realised that wasn't what i ment :see_no_evil:
is it okay if i send you my code in a message ?? i have a feeling we miscommunicate somewhere.


#18

yea, you can send it. Yea i think we do, because the code you posted is as original from the exercise, without any additional code


#19

Guys, I've finished mine and that's the only way I could make it work, using return before calling the function. Also, it didn't work when I was calling the function compare() inside the function choose(). I don't know if it's a recursive matter...

var userChoice;
var computerChoice;

function choose() {
    userChoice = prompt("Do you choose rock, paper or scissors?");
    console.log ("User: " + userChoice);

    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! Choose again!");
        choose ();
        return compare (userChoice, computerChoice);
    } else 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 {
        console.log ("You have to choose among the 3 options!");
        choose ();
        return compare (userChoice, computerChoice);
    }
}

choose ();
compare (userChoice, computerChoice);

#20

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