I'm currently learning javascript and I keep having this error!!!
This is my script:
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";
}
Should be:
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";
}
Or neater:
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"
}
}
Related
I'm just trying to build the first two steps of my r/p/s game, and here is my coding so far. It is giving me the response "Syntax error: unexpected token else" and I cannot for the life of me figure out why...
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";
}
}
You closed the main if/else chain with your paper wins section, I believe you meant to attach that else to the inner if/else as below.
In the paper section you had the same thing, I fixed that instance as well. From here you should be able to add the final if(choice1 === "scissors") segment.
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";
}
}
I fixed this for you:
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";
}
}
}
You've got a muddle of ifs and elses!
I believe specifically, you've got if (choice2 === "scissors") without curly braces, which is fine, except then you closed the brackets you never opened with an } else {. You should indent properly, it will help you see why you might have run into an issue.
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";
}
}
}
So you can see, I added { after any 'ifs' without, and then added two } to close those in the correct spots.
else {
return "Paper wins";
}
else if (choice1 === "paper") {
you cannot do a else if after doing a else;
I was negated by not giving the full answer and force him to study conditions?
I am having a problem of which I get a syntax error (Unexpected else)
Below is my code. I think it would be a "curly bracket" or semi-colon problem
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 compare = function(choice1, choice2) {
if (choice1 === choice2) {
return "The result is a tie!";
} else if (choice1 === "rock") {
if (choice2 === "scissors") {
return "rock swins";
} 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"
}
}
};
Any help would be appreciated. Thanks
Please place below code, I added one missing close bracket for last else part else if (choice1 === "paper").
var compare = function(choice1, choice2) {
if (choice1 === choice2) {
return "The result is a tie!";
} else if (choice1 === "rock") {
if (choice2 === "scissors") {
return "rock swins";
} 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"
}
}
}
};
Let me know if there is any error remain.
I just completed a few exercises on "CodeAcademy" which taught me to create a rock,paper and scissors game. Everything works fine except the output that I want to receive. I am not getting the out "paper wins". All i get it "paper". I have just started to grasp the foundation of Javascript which is why I am not a strong scripter yet.
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random(0);
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!");
}
if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
} else {
if (choice2 === "scissors") {
return "scissors wins";
}
}
if (choice1 === "scissors") {
if (choice2 === "rock") {
return "rock wins";
} else {
if (choice2 === "paper") {
return "scissors wins";
}
}
}
}
};
I think you are invoking the compare function on selection of the choices. so you are sending user choice to the function. now you should compare it with computerChoice. what is choice2 there?
As others have stated you never call your compare function and you never return any value of that function. And even if you did your function would fail most of the time anyway.
You structure:
if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
} else {
if (choice2 === "scissors") {
return "scissors wins";
}
}
if (choice1 === "scissors") { //is never true because choice1 is always "paper"
if (choice2 === "rock") {
return "rock wins";
} else {
if (choice2 === "paper") {
return "scissors wins";
}
}
}
} //if choice1 === "paper" ends here
//no love for the rock?
What you want to achieve:
if (choice1 === "paper") {
if (choice2 === "rock")
{
return "paper wins";
}
else if (choice2 === "scissors")
{
return "scissors wins";
}
}
else if (choice1 === "scissors")
{
if (choice2 === "rock")
{
return "rock wins";
}
else if (choice2 === "paper")
{
return "scissors wins";
}
}
else if (choice1 === "rock") //don't forget the rock
{
if (choice2 === "paper")
{
return "paper wins";
}
else if (choice2 === "scissors")
{
return "rock wins";
}
}
Then you will have to call your function like this:
var result = compare(userChoice, computerChoice);
And you log the result variable. In your example you logged computerChoice which can only be "paper", "scissors" or "rock" because that is the value you give it, not the value of the function.
Since you let the user type in anything he/she wants you will obviously also have to validate their input or the if/else structure will fail.
So I'm doing the JS codecademy course and I'm on the rock paper scissors thing, and I've seen other topics, but I don't have misplaced semicolons as far as I know. Can you tell me what's wrong?
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";
}
}
}
}
looks like you messed up the braces:
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";
} // this was missing
}else if(choice1 === "paper"){
if(choice2 === "rock"){
return "paper wins";
}else{
return "scissors wins";
}
}
}
you have an else followed by an else if:
else{
return "paper wins";
}else if(choice1 === "paper"){
if(choice2 === "rock"){
return "paper wins";
}
I'm on a codecademy lesson (found here) but keeps telling me "Your code returned 'rock wins' instead of 'paper wins' when the inputs are paper and rock", why? It should be correct. Since it's talking about 'rock wins' then it's talking about rock vs scissors. So why is it saying that "instead of paper wins" when there isn't even paper involved in the only outcome of 'rock wins'?
var compare = function (choice1, choice2) {
if (choice1 === choice2) {
return("The result is a tie!");
}
if (choice1 === "rock") {
if (choice2 === "scissors");
} else {
return ("rock wins");
}
if (choice1 === "paper") {
if (choice2 === "rock");
} else {
return ("paper wins");
}
if (choice1 === "paper") {
if (choice2 === "scissors");
} else {
return ("scissors wins");
}
};
Look at your first condition:
if (choice1 === "rock") {
if (choice2 === "scissors");
} else {
return ("rock wins");
}
So if choice1 is rock, you enter the if-block (which doesn't actually return anything, but since in this case choice1 is actually "paper" it goes into the else-block, which unconditionally returns "rock wins". Try refactoring it to be something like this:
if (choice1 === choice2) {
return("The result is a tie!");
}
if (choice1 === "rock") {
if (choice2 === "scissors") {
return ("rock wins");
} else {
return ("paper wins");
}
}
if (choice1 === "paper") {
if (choice2 === "rock") {
return ("paper wins");
} else {
return ("scissors wins");
}
}
if (choice1 === "paper") {
if (choice2 === "scissors") {
return ("scissors wins");
} else {
return ("rock wins");
}
}
But hey, let's get fancy. Try putting your choices into an array:
var choices = ["rock", "paper", "scissors"];
Now, notice that the item to the right always beats the item to the left (if we consider that the array wraps around). How can we use this to simplify the code? Well we can just compare the indexes of each choice, taking care to handle the edge case of scissors vs. rock:
var x = choices.indexOf(choice1),
y = choices.indexOf(choice2);
if (x === y) {
return("The result is a tie!");
} else if (x > y) {
if (x == 3 && y == 0) {
return choice2 + " wins";
} else {
return choice1 + " wins";
}
} else {
return choice2 + " wins";
}
But we can use the remainder operator (%) here to handle the edge case more easily:
var choices = ["rock", "paper", "scissors"];
var compare = function (choice1, choice2) {
var x = choices.indexOf(choice1),
y = choices.indexOf(choice2);
if (x === y) {
return("The result is a tie!");
}
return (((x - y) % 3) > 0 ? choice1 : choice2) + " wins";
}
Your function will always return "rock wins" when choice1 is not "rock". This is because you have used the if - else statement.
What you are doing is:
if choice1 is rock do something
else return "rock wins"
I will give you the first statement:
if (choice1 === "rock") {
if (choice2 === "scissors") return ("rock wins");
if (choice2 === "paper") return ("Paper wins");
}
jsFiddle Demo
There are interesting choices you made when using the if statements. There shouldn't be semi colons after them. Also, the logic composition can struggle when using many if then else statements. Often, it is best to use a switch case statementMDN in these situations.
var compare = function (choice1, choice2) {
if(choice1==choice2)return "The result is a tie!";
switch(choice1+choice2){
case "rockscissors": case "scissorsrock":
return "rock wins";
case "rockpaper": case "paperrock":
return "paper wins";
default: return "scissors wins";
}
};
if (choice1 === "rock") {
if (choice2 === "scissors");
} else {
return ("rock wins");
}
Look at this again. You say:
IF choice1 === rock, THEN
IF choice2 === scissors THEN DO NOTHING
ELSE (choice1 is not rock)
return 'rock wins'
This is a case where explicit braces would help. I'm guessing you meant to do this:
if (choice1 === "rock") {
if (choice2 === "scissors") {
}
} else {
return ("rock wins");
}
if the data is very controlled, you can just do:
if in (choice1+""+choice2) you find "ks", "rr" or "sp", you choice1
won, else lost (example below)
function getWinner(choice1, choice2){
var both_str, after_removing;
if(choice1 == choice2){
return "The result is a tie!";
}
both_str = (choice1 + "" + choice2);
after_removing = both_str.replace(RegExp("ks|rr|sp", "g"), "");
return (choice1 + ((both_str.length - after_removing.length) ? " won" : " lost"));
}
And you get the following results:
console.log(getWinner("scissors", "paper")); //scissors won
console.log(getWinner("rock", "scissors")); //rock won
console.log(getWinner("paper", "rock")); //paper won
console.log(getWinner("scissors", "rock")); //scissors lost
console.log(getWinner("rock", "paper")); //rock lost
console.log(getWinner("paper", "scissors")); //paper lost
console.log(getWinner("scissors", "scissors")); //The result is a tie!
console.log(getWinner("rock", "rock")); //The result is a tie!
console.log(getWinner("paper", "paper")); //The result is a tie!