Need som help with a spin-off on this project


#1

This is still a work in progress, but I'm stuck at trying to get my while loop to work correctly.
I've figured that I should probably drop the entire while loop and the if\ else and replace it all with cases and a default, but for the sake of learning, I'd like to figure out why my code isn't running.
I've commented out the loop that I can't get to work.

Edit: I've tried it out a bit and part of my problem was solved, so I changed the code. The while loop now stops if I type in an "correct" value into userChoice. However, if I write an "incorrect" answer, the second prompt command will show and work as intended, but userChoice will still be the first input, and the new one won't be taken into consideration.

Here's my code so far, any help would be very much appreciated:

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

Old code: //while (userChoice != "rock" || "paper" || "scissors" || "lizard" || "Spock") {
//prompt("Please choice one of these alternatives: rock, paper, scissors, lizard or Spock:");
//}
// If userChoice is NOT any of the alternatives, make user choice again.

New code:

//while (userChoice != "rock" && userChoice "paper" && userChoice "scissors" && userChoice "lizard" && userChoice "Spock") {
//prompt("Please choice one of these alternatives: rock, paper, scissors, lizard or Spock:");
//}
// If userChoice is NOT any of the alternatives, make user choice again.

var computerChoice = Math.random();
if (computerChoice < 0.20) {
computerChoice = "rock";
} else if(computerChoice <= 0.40) {
computerChoice = "paper";
} else if (computerChoice <= 0.60) {
computerChoice = "scissors";
} else if (computerChoice <= 0.80) {
computerChoice = "lizard";
} else {
computerChoice = "Spock";
};
console.log("Computer choose: " + computerChoice);

var win = "You win!";
var lose = "You lose!";

var compare = function (choice1, choice2) {
if (choice1 === choice2) {
return ("The result is a tie!");
} else if (choice1 === "rock") {
if (choice2 === "scissors") {
return ("Rock crushes scissors." + " " + win);
} else if (choice2 === "paper") {
return ("Paper covers rock." + " " + lose);
} else if (choice2 === "lizard") {
return ("Rock crushes lizard." + " " + win);
} else if (choice2 === "Spock") {
return ("Spock vapourises rock." + " " + lose)
}
} else if (choice1 === "paper") {
if (choice2 === "rock") {
return ("Paper covers rock." + " " + win);
} else if (choice2 === "scissors") {
return ("Scissors cuts paper." + " " + lose);
} else if (choice2 === "lizard") {
return ("Lizard eats paper." + " " + lose);
} else if (choice2 === "Spock") {
return ("Paper disproves Spock." + " " + win);
}
} else if (choice1 === "scissors") {
if (choice2 === "rock") {
return ("Rock crushes scissors." + " " + lose);
} else if (choice2 === "paper") {
return ("Scissors cuts paper." + " " + win);
} else if (choice2 === "lizard") {
return ("Scissors decapitates lizard." + " " + win);
} else if (choice2 === "Spock") {
return ("Spock crushes scissors." + " " + lose);
}
} else if (choice1 === "lizard") {
if (choice2 === "rock") {
return ("Rock crushes lizard." + " " + lose);
} else if (choice2 === "paper") {
return ("Lizard eats paper." + " " + win);
} else if (choice2 === "scissors") {
return ("Scissors decapitates lizard." + " " + lose);
} else if (choice2 === "spock") {
return ("Lizard poisons Spock." + " " + win);
}
} else if (choice1 === "Spock") {
if (choice2 === "rock") {
return ("Spock vapourises rock." + " " + win);
} else if (choice2 === "paper") {
return ("Paper disproves Spock." + " " + lose);
} else if (choice2 === "scissors") {
return ("Spock crushes scissors." + " " + win);
} else if (choice2 === "lizard") {
return ("Lizard poisons Spock." + " " + lose);
}
}
};

compare(userChoice, computerChoice);


#2

Never mind, I figured it out :grinning:
I forgot to make it so that the second prompt gives userChoice the new value! :blush:
This code works:

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

while (userChoice != "rock" && userChoice != "paper" && userChoice != "scissors" && userChoice != "lizard" && userChoice != "Spock") {
var userChoice = prompt("Please choice one of these alternatives: rock, paper, scissors, lizard or Spock:");
break;
}
// If userChoice is NOT any of the alternatives, make user choice again.

var computerChoice = Math.random();
if (computerChoice < 0.20) {
computerChoice = "rock";
} else if(computerChoice <= 0.40) {
computerChoice = "paper";
} else if (computerChoice <= 0.60) {
computerChoice = "scissors";
} else if (computerChoice <= 0.80) {
computerChoice = "lizard";
} else {
computerChoice = "Spock";
};
console.log("Computer choose: " + computerChoice);

var win = "You win!";
var lose = "You lose!";

var compare = function (choice1, choice2) {
if (choice1 === choice2) {
return ("The result is a tie!");
} else if (choice1 === "rock") {
if (choice2 === "scissors") {
return ("Rock crushes scissors." + " " + win);
} else if (choice2 === "paper") {
return ("Paper covers rock." + " " + lose);
} else if (choice2 === "lizard") {
return ("Rock crushes lizard." + " " + win);
} else if (choice2 === "Spock") {
return ("Spock vapourises rock." + " " + lose)
}
} else if (choice1 === "paper") {
if (choice2 === "rock") {
return ("Paper covers rock." + " " + win);
} else if (choice2 === "scissors") {
return ("Scissors cuts paper." + " " + lose);
} else if (choice2 === "lizard") {
return ("Lizard eats paper." + " " + lose);
} else if (choice2 === "Spock") {
return ("Paper disproves Spock." + " " + win);
}
} else if (choice1 === "scissors") {
if (choice2 === "rock") {
return ("Rock crushes scissors." + " " + lose);
} else if (choice2 === "paper") {
return ("Scissors cuts paper." + " " + win);
} else if (choice2 === "lizard") {
return ("Scissors decapitates lizard." + " " + win);
} else if (choice2 === "Spock") {
return ("Spock crushes scissors." + " " + lose);
}
} else if (choice1 === "lizard") {
if (choice2 === "rock") {
return ("Rock crushes lizard." + " " + lose);
} else if (choice2 === "paper") {
return ("Lizard eats paper." + " " + win);
} else if (choice2 === "scissors") {
return ("Scissors decapitates lizard." + " " + lose);
} else if (choice2 === "spock") {
return ("Lizard poisons Spock." + " " + win);
}
} else if (choice1 === "Spock") {
if (choice2 === "rock") {
return ("Spock vapourises rock." + " " + win);
} else if (choice2 === "paper") {
return ("Paper disproves Spock." + " " + lose);
} else if (choice2 === "scissors") {
return ("Spock crushes scissors." + " " + win);
} else if (choice2 === "lizard") {
return ("Lizard poisons Spock." + " " + lose);
}
}
};

compare(userChoice, computerChoice);


#3

var userChoice = prompt("Do you choose rock, paper, scissors, lizard or Spock?");
while (userChoice != "rock" && userChoice != "paper" && userChoice != "scissors" && userChoice != "lizard" && userChoice != "Spock") {
    var userChoice = prompt("Please choice one of these alternatives: rock, paper, scissors, lizard or Spock:");
    break;
}

first of all do you really need the break? if it is correct you break anyway because of your condition and if it is not you want to keep on looping, do you? Also you could think about a do/while loop, this way you could save one prompt:

do{
var userChoice = prompt(...);
} while(...)

#4

Thanks, that's true, the break is unnecessary for the code to run.
I also agree on the do\ while loop thing, it cleans up the code a bit.

So, now I'm on to my next challenge in this project. I have to more steps that I want to complete before I start animating some of it.
The first part is to make the user able to choose again if the game ends in a draw. I've tried several different codes, but I can't seem to find one that works. Here's some of what I've tried:

var compare = function (choice1, choice2) {
while (choice1 === choice2) {
var userChoice = prompt("The result is a tie! Choose again:");
continue;

This code will let me run the program and it works fine as long as the game doesn't end in a tie. If the game ends in a tie, I get my new prompt up (which is correct), but no matter what I type in now, the new prompt seem to come up in an endless loop. So my probably seems to be that my new prompt doesn't change the outcome of the userChoice compared to the computerChoice and therefore it keeps comparing the old choices.

I've also tried this:

var compare = function (choice1, choice2) {
if (choice1 === choice2) {
console.log("The result is a tie!");
var userChoice = prompt("Choose again:");

Which also works to some extent. I get prompted again, but the result stays a tie and the game ends and ignores the new prompt answer.

And this:

while (choice1 === choice2) {
console.log("The result is a tie!");
var userChoice = prompt("Choose again:");

Puts me in an infinite loop of printing: "The result is a tie!" whenever the game comes to a draw.

So.. Is there anyone that can point me in the right direction? :smile:

My next challenge will be to ask the user if he\ she wants to play again, and if yes, then restart the game.


#5

Ok first of all I wouldn't do this in the compare method at all. The compare function is so nice an compact and works for any string input that I'd just catch the output of this function and compare it to the tie result and in this case I'd let the user choose again and run the compare function once more with the change input. I guess you're able to translate this to code yourself, are you?

Now to your loops:

var compare = function (choice1, choice2) {
    while (choice1 === choice2) {
        var userChoice = prompt("The result is a tie! Choose again:");
        continue;

the problem is that your while loop checks choice1 and choice2 but your prompt changes userChoice. Also just changing userChoice would be mean for the computer wouldn't it?