Rock, Paper, Scissors Advanced


#1

Okay, so, I'm on the build "Rock, Paper, Scissors" 9."Next Steps" lesson, and I'm trying to make it so that the program loops back to the beginning when someone types in an invalid statement. I've got that figured out, but now it's giving me a "TypeError: Object Expected" message whenever I run the code. I don't know what that means, and this engine(Is that the right term?) doesn't tell me where the problem is. Any help is appreciated.


/*To kick things off*/
var play = function(){
userChoice = prompt("Do you choose rock, paper or scissors?");
check(userChoice);
}

/*To keep things going after the check*/
var continu = function(){
computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "rock"
	compare(userChoice, computerChoice);
} else if(computerChoice <= 0.67) {
	computerChoice = "paper"
	compare(userChoice, computerChoice);
} else {
	computerChoice = "scissors"
	compare(userChoice, computerChoice);
}

var compare = function(userChoice, computerChoice){
    /*If the choices are identical, it is a tie*/
    if(userChoice === computerChoice){
        return("The result is a tie!")
    }
    /*If userChoice is rock*/
    else if(userChoice === "rock"){
        /*and computerChoice is scissors, rock wins.*/
        if(computerChoice === "scissors"){
            return("rock wins")
        }
        /*But if computerChoice is paper, paper wins.*/
        else{
            return("paper wins");
        }
    }
    /*If userChoice is paper*/
    else if(userChoice === "paper"){
        /*and computerChoice is rock, paper wins.*/
        if(computerChoice === "rock"){
            return("paper wins");
        }
        /*But if computerChoice is scissors, scissors wins.*/
        else{
            return("scissors wins");
        }
    }
    /*If userChoice is scissors*/
    else if(userChoice === "scissors"){
        /*and computerChoice is rock, rock wins.*/
        if(computerChoice === "rock"){
            return("rock wins");
        }
        /*But if computerChoice is paper, scissors wins*/
        else{
            return("scissors wins");
        }
    }
}
}

/*To handle if the user doesn't input rock, paper, or scissors*/
var check = function(userChoice){
    if(userChoice === "rock"){continu()}
    else if(userChoice === "scissors"){continu()}
    else if(userChoice === "paper"){continu()}
    else{
        play();
    }   
}
play();


#2

On a side note, the return function isn't logging anymore. Was it coded in for convenience previously in the lesson, and now I need to console.log as well as return to see who won?


#3

Okay, I have determined that it has not stopped logging at random, but is dead-ending somewhere without an error message. I did this by placing a console.log command under every return in the compare function, so that(theoretically) it would return and then console.log. This was after I created a continu2 function whos sole purpose was to call the compare function, making it so that continu called it instead. When I did that, it gave me a "compare is not defined" error message, so I moved continu2 under compare, watched it dead-end a few times, moved continu2 back, and now here we are.


#4

can you post an updated version of your code?


#5

Right here. The console.logs were removed as well, and I didn't want to put them back.

/*To kick things off*/
var play = function(){
userChoice = prompt("Do you choose rock, paper or scissors?");
check(userChoice);
}

var continu2 = function(){
    compare(userChoice, computerChoice);
}

/*To keep things going after the check*/
var continu = function(){
computerChoice = Math.random();
if (computerChoice <= 0.34) {
	computerChoice = "rock"
	continu2();
} else if(computerChoice >= 0.67) {
	computerChoice = "paper"
	continu2();
}
else {
	computerChoice = "scissors"
	continu2();
}
}

var compare = function(userChoice, computerChoice){
    /*If the choices are identical, it is a tie*/
    if(userChoice === computerChoice){
        play();
    }
    /*If userChoice is rock*/
    else if(userChoice === "rock"){
        /*and computerChoice is scissors, rock wins.*/
        if(computerChoice === "scissors"){
            return("rock wins");
        }
        /*But if computerChoice is paper, paper wins.*/
        else{
            return("paper wins");
        }
    }
    /*If userChoice is paper*/
    else if(userChoice === "paper"){
        /*and computerChoice is rock, paper wins.*/
        if(computerChoice === "rock"){
            return("paper wins");
        }
        /*But if computerChoice is scissors, scissors wins.*/
        else{
            return("scissors wins");
        }
    }
    /*If userChoice is scissors*/
    else if(userChoice === "scissors"){
        /*and computerChoice is rock, rock wins.*/
        if(computerChoice === "rock"){
            return("rock wins");
        }
        /*But if computerChoice is paper, scissors wins*/
        else{
            return("scissors wins");
        }
    }
}

/*To handle if the user doesn't input rock, paper, or scissors*/
var check = function(userChoice){
    if(userChoice === "rock"){continu()}
    else if(userChoice === "scissors"){continu()}
    else if(userChoice === "paper"){continu()}
    else{
        play();
    }   
}
play();

#6

right, here we go:

/*To kick things off*/
var play = function(){
userChoice = prompt("Do you choose rock, paper or scissors?");
return check(userChoice);
}

var continu2 = function(){
    return compare(userChoice, computerChoice);
}

/*To keep things going after the check*/
var continu = function(){
computerChoice = Math.random();
if (computerChoice <= 0.34) {
    computerChoice = "rock"
} else if(computerChoice >= 0.67) {
    computerChoice = "paper"
}
else {
    computerChoice = "scissors"
    
}
   return continu2();
}

var compare = function(userChoice, computerChoice){
    /*If the choices are identical, it is a tie*/
    if(userChoice === computerChoice){
        // alert("The result is tie!");
        console.log("The result is a tie!");
        return play();
    }
    /*If userChoice is rock*/
    else if(userChoice === "rock"){
        /*and computerChoice is scissors, rock wins.*/
        if(computerChoice === "scissors"){
            return("rock wins");
        }
        /*But if computerChoice is paper, paper wins.*/
        else{
            return("paper wins");
        }
    }
    /*If userChoice is paper*/
    else if(userChoice === "paper"){
        /*and computerChoice is rock, paper wins.*/
        if(computerChoice === "rock"){
            return("paper wins");
        }
        /*But if computerChoice is scissors, scissors wins.*/
        else{
            return("scissors wins");
        }
    }
    /*If userChoice is scissors*/
    else if(userChoice === "scissors"){
        /*and computerChoice is rock, rock wins.*/
        if(computerChoice === "rock"){
            return("rock wins");
        }
        /*But if computerChoice is paper, scissors wins*/
        else{
            return("scissors wins");
        }
    }
}

/*To handle if the user doesn't input rock, paper, or scissors*/
var check = function(userChoice){
    if(userChoice === "rock" || userChoice === "scissors" || userChoice === "paper" ){ return continu();}
    else{
        return play();
    }   
}
console.log(play());

this should work, the biggest problem where the recursive function calls. The moment you call a function from inside a function, you should add return to this function call (the inner one). otherwise, the result of this function isn't returned, which means in the end, the value doesn't get logged to the console. given one of the inner function calls, didn't return

I might also might have made some optimizations to your code


#7

Oh. Oh wow. That makes so much sense. I can't believe that I didn't think of that. Thanks!


#8

i know this explanation about function calling other functions is a bit vague, you understand it? also, the function call on the last line:

console.log(play());

i use console.log(), so it gets logged to the console, take a good look at the code, i made some changes


#9

Absolutely. And I hadn't even noticed that the "The result is a tie" line had been removed from my code, either, so thanks!


#10

the problem is that if you call the function again, there is no mention of the result is a tie, i also suggested (see the comment) you could use an alert instead.

No further questions? And everything works as you want? That would be like the ideal situation :slight_smile:


#11

Unfortunately, you've opened up a whole new can of worms with the whole alert thing XD

First and foremost, why won't it log again if the result is a tie? That doesn't make sense to me.

Second, is an alert a popup? Sorry, this is probably a dumb question, but I haven't gotten to alerts yet. I'm only on lesson 5 for 'Search text for your name' atm.

Third, is the alert format any different from a console.log? IE, do I just type 'alert(message);'? Or something else?

Thanks for your help and patience :slight_smile:


#12

it does if you use console.log?

i don't think alerts are covered anywhere in the course. They are popups just like prompt. but instead with a message only.

i showed you how to do an alert? It was as comment in the code


#13

Hi, I understand the basic version of the game, but can't get the advanced version's code above. Sorry for noob questions in advance:
1. why is it necessary to wrap vars userchoice and computerchoice with functions and add a play function?
2. why the function continu2 that compares userchoice and computerchoice stands before var continue. where we get computerchoice?
2. why the var check is placed at the end of the program while it should check userchoice in the very beginning?


#14

because then you can call the play function again if the user enters something invalid, prompting them for valid input again. I did not design the program.

doesn't matter? you call continue2 after continue, so is fine. Take a good look at the function calls.

again, doesn't matter? You can just call check whenever you need it. I didn't design the program, i would have done it different altogether. Using loops which aren't covered yet.


#15

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