Hey! I’m trying to write a program for a math class, and for some reason it isn’t working. Whenever I type “9”, I should survive, but it says I died. Here is my code:
confirm(“Hello! Welcome to Numberquest! You will need two dice to play. This is a text-based ‘Choose your own Adventure’ game. During the game, you will come upon ‘Choices’ where the way you will go will be dictated by your choice and a roll of the dice. Good luck, and have fun!”);
confirm(“You are a knight on a quest to slay a fearsome dragon! To defeat the dragon, you must go to his lair on a mountain, and fight him! (Push ‘OK’ to continue!)”);
var answerOne = prompt(“There are two roads to the mountain. On the first road (A), if you roll a 5, 6, 7, or 8, you fall into a pit! On the second road, (B) if you roll a 9, 10, 11, or 12, you will be crushed by a rock! Which road do you take, A or B? (Please answer in all caps!)”);
if (answerOne = “A”) {
var answerTwoA = prompt(“Roll the dice! What number did you get?”);
if (answerTwoA = “5” || “6” || “7” || “8”) {
confirm(“Alas! You fell in the pit and died! Try again?”);
}
else {
confirm(“You survived! Onward!”);
}
}
That is assignment, look up what operator is used to compare two values for equality.
Keep in mind that the interpreter is not a human. It does exactly what you say for it to do, not what you mean for it to do, no matter how obvious it is what you mean.
And to look up how to do those things, you would consider what operation you want to perform, for example “javascript compare two strings” or “javascript OR” and then let some search engine find you the relevant information
|| is an operator that is used to combine boolean values. So on each side of it you should have an expression that evaluates to true, or false and "6" is neither of those.
You would do:
if (my_cat_is_blue || my_cat_is_purple) {
...
Not:
if (my_cat_is_blue || "kebab") {
...
"kebab" is not a true/false value, it’s a string, doesn’t make sense to use with OR
As so happens, it will be considered to be true, because it is not empty.