Anyone else complete the 'What's Next' portion?


#1

Here is one working solution, although I did not make 'rope' a choice. Hope this is helpful to those who may be stuck.

var roshamboGame = 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 {
	    computerChoice = "scissors";
    } console.log("Computer: " + computerChoice);
    
    var result = compare(userChoice, computerChoice);
    console.log(result);
    if(result === "Choose again!")
        roshamboGame();
}

var compare = function(choice1, choice2){
    if(choice1 === choice2){
        return("Choose again!");
    }
    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{
        return ("You made an invalid choice. By default, the computer wins!");
    }
}

roshamboGame();

Has anyone else gotten this to work using a different structure? Would love to see other examples. Thanks!


#2

Mine is similar and I am curious to know if there is a better/efficient way to do it.

    var game = function () {
        var userChoice = prompt("Do you choose rock, paper, rope or scissors?");
        userChoice = userChoice.toLowerCase();
        var test = true;
        //if choices are correct the flag or test is set to true so the while loop isn't       engaged
        if(userChoice==='rock' || userChoice==='scissors' || userChoice==='paper' || userChoice==='rope'){
            test = true;
        }
        else {
            //if the choice typed is incorrect flag remains false
            test = false;
        }
        while(test === false){
            if(userChoice ==='rock' || userChoice==='rope' || userChoice ==='scissors' || userChoice === 'paper'){
                test = true;
             }
            else {
                //when user needs to reinput
               userChoice = prompt("Error! Choose from rock, paper, rope or scissors?");                    userChoice = userChoice.toLowerCase();
                test = false;
            }
        };
        
    var computerChoice = Math.random();
        if (computerChoice < 0.26) {
        	computerChoice = "rock";
        } 
        else if(computerChoice < 0.51) {
        	computerChoice = "paper";
        } 
        else if(computerChoice < 0.76) {
        	computerChoice = "rope";
        } 
        else {
        	computerChoice = "scissors";
        } 
        console.log("Computer: " + computerChoice);
        
        var result = compare (userChoice, computerChoice); 
            console.log (result);
            if (result === "The result is a tie! Choose again") {
                game ();
            }
    };
      
        
        //COMPARE CHOICES
    var compare = function (choice1, choice2) {
        if (choice1 === choice2) {
            alert("The result is a tie! Choose again");
            return "The result is a tie! Choose again";
        }
        else if (choice1 === "rock") {
            if (choice2 === "scissors" || choice2 === "rope") {
                return "rock wins";
            }
            else {
                return "paper wins";
            }
        }
        else if (choice1 === "paper") {
            if (choice2 === "rock" || choice2 === "rope") {
                return "paper wins";
            }
            else {
                return "scissors wins";
            }
        }
        else if (choice1 === "scissors") {
            if (choice2 === "rock") {
                return "rock wins";
            }
            else {
                return "scissors wins";
            }
        }
        else if (choice1 === "rope") {
            if (choice2 === "rock") {
                return "rock wins";
            }
            else {
                return "scissors wins";
            }
        }
    };
    game ();

#3

Hi, thanks for posting this. super helpful for me.
I'm confused why it works though - isn't the var userChoice used inside the "game" function a local element? Yet you are able to call userChoice as a parameter in var result = compare.


#4

I'm calling that inside the game() function itself and not outside. This is why it works..
Hope it helps. :slight_smile:


#6

I love this solution! I was seriously over-complicating it, but your solution is great!