New to javascript: confirmation / code
I am starting to use Codex Academy to learn and I am trying to write a simple JavaScript game just to get my understanding. I cannot decipher why my first few lines of code are not working. It should be obvious what I'm trying to do, but the confirmation commands don't work at all.
Here's the code:
var classChoice=prompt("You need to choose a class. Will you play as a wizard or as a warrior?").toLowerCase;
if (classChoice==="wizard"){
confirm("You have chosen wizard. Is this correct? (yes or no)");
}else if (classChoice==="warrior"){
confirm("You have chosen warrior. Is this correct?");
}
Can anyone comment?
source to share
In your code .toLowerCase
, correctly written in camelCase, does not have the required parentheses ()
at the end of the name invocation. So, essentially without the parentheses, you are just trying to provide the contents of the function, not refer to it.
In addition, it is probably best not to trust the user at all to enter the required information correctly, at least not 100% of the time. Hence why are you trying to use the function .toLowerCase()
in the first place - to reduce the chance of human error.
Therefore, as stated in one of the other answers, you must have an else block, like:
else {
// Handle input that does not match either choice
}
source to share