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?

+3


source to share


3 answers


toLowerCase

- a function that you have to call: .toLowerCase()

.




Note: what happens when they enter a value other than the 2 you are checking ...?

+6


source


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
}

      

+3


source


100% of all functions must end with (), otherwise the browser will treat it as a variable

+1


source







All Articles