9. Next Steps: Asking for new choices during a tie


#1


Build "Rock, Paper, Scissors" Exercise 9:
"In this version, if both players make the same choice, the game returns a tie. What if the game didn't end there but instead asked both players for new choices?"


Here is my attempt at completing the exercise above. I'm posting it here as reference. Any comments to improve are welcome too!


var userChoice

function valid(){
    userChoice = prompt("Do you choose rock, paper or scissors?");
    if(userChoice === "rock"){
        return "rock";
    } else if(userChoice === "paper"){
        return "paper";
    } else if(userChoice === "scissors"){
        return "scissors";
    } else{
        return valid();
    }
}

userChoice = valid();

var computerChoice

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

compute();

function compare(choice1, choice2){
    if(choice1 === choice2){
        console.log("Tie. Do over.");
        valid();
        compute();
        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";
        }
    }
}

compare(userChoice, computerChoice);


#2

Actually quite nice!
But although it is possible I'd rather encourage you to go on and learn about loops than to use recursion (calling functions inside itself) to this extend. The problem is that function are some sort of sub-program meaning each function call interrupts the execution flow and instead works on the function that has been called. This means that you stack up sub-programs until you reach a return that is just a value e.g. made a correct input in your valid function. So if you have 1000 failed attempts this means that you have 1000 open functions waiting in the background to be finished. That is possible and in fact it's impressive how you hacked this to be a pseudo loop but when you continue with the exercises you'll get to know actual loops that are more suitable for this purpose :slight_smile:

and the other thing you could improve is to avoid global variables. When your code gets longer it will be really hard to keep track of variables that are used in 1000 different places around the program so as a rule of thumb make your variables "as local as possible and as global as necessary.

For example your valid function is designed to return a valid user input when called. That feature is used here:

userChoice = valid();

but inside of your tie case in the compare function you just use this:

valid();

which makes use of the fact that the function also manipulates the global variable userChoice and is only able to leave the function if it is valid. That is a whole different design concept and it makes it more difficult to follow what value is stored in userChoice over the course of your program. So I'd rather recommend using the first idea of returning the value and making it as local as possible, this way you can look at a certain function and are able to understand it without the need to comprehend the whole program. Also think about what happens if two human players compete.
With the first approach of returning a valid input you could go by:

var player1 = valid();
var player2 = valid();

and that is it 2 players,2 choices and one function. Whereas the second way wouldn't work as valid only manipulates 1 variable. PS: To be fair it would probably still be possible but not as neat as the other one:

valid();
var player1 = userChoice;
valid();
var player2 = userChoice;

#3

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