Rock paper scissors Out of Scope and no Compare


#1

My goal is to terminate the program if rock paper or scissors is not entered into the prompt, and for some reason the computerMove variable is said to be used out of scope, which I'm not entirely sure of what it means as well as the compare function I created which doesn't work at all

userChoice = prompt("Do you choose rock, paper or scissors?");

if (userChoice !== "rock","paper", "scissors")

{console.log("You chose " + userChoice + "!");

console.log(" ");

}

else

{

alert("That wasn't one of the options");

end();

}

setTimeout(function(){

console.log("Aaaand...");

}, 2000

);

setTimeout(function(){

var computerChoice = Math.random();

/*{

console.log(computerChoice);

}*/

console.log("the computer chose ");

if (computerChoice <= 0.33)

{var computerMove =  console.log("rock!");

}

else

{

if (computerChoice < 0.67) {computerMove =  console.log("paper!");

}

else

{computerMove = console.log("scissors!");}}}, 5000);

var compare = function(choice1, choice2) {

if(choice1 === choice2)

{

return "This means the result is a tie";

}

};

compare(computerMove, userChoice);

So the compare doesn't work, the computerMove is out of scope, and I'm not sure what's the deal with the exit... isn't that supposed to end the program? I'm not sure how to use that, but I tried... Any help would be appreciated!


#2

//This is what I ended up with after doing the rock, paper, scissors exercise.
// Tried explaining to you before where you were making the errors but guess you don't understand.
// This should help you in at least finishing the exercise.

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

var computerChoice = Math.random();
console.log("Computer creates random # " + computerChoice);

if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
console.log("Computer changes # to object: " + 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 === "paper") {
return "scissors wins";
}else {
return "rock wins";
}
}
};

compare(userChoice, computerChoice);


#3

(userChoice !== "rock", "paper", "scissors")

Have you got something to support that !== will do what you want in this situation? You kind of made that up. You can certainly experiment, but when you do, be sure to test it. The computer will not do what you want, it'll do what you say.

Generally, test each individual piece of code by itself if you're unsure. And/or look up some documentation/examples for reference.

I also suggest cleaning up the formatting, having it that way most likely hurts your ability to reason about it, and certainly doesn't help others read it. This isn't even something you do after writing code, it's something to maintain at all times.

What you'd do is compare the input to all the valid ones, and if there's no match, handle that. You're not doing that though, as mentioned above.


#4

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