javascript - Uncaught TypeError: Cannot read property 'toUpperCase' of null -
my code seems right don't know why getting error:
uncaught typeerror: cannot read property 'touppercase' of null
here code:
//the function executed after clicks "take quiz" function startquiz() { //the variable first question var firstanwser = prompt("who posted first youtube video?"); //the if statement first question if (firstanwser.touppercase() === 'jawed karim') { //if person correct dialog box says correct pops alert("correct"); //the variable second question var secondanwser = prompt("when domain name youtube.com activated?"); if (secondanwser.touppercase() === 'febuary 14, 2005') { alert("correct"); var thirdanwser = prompt("what first video on youtube called?"); if (thirdanwser.touppercase() === 'me @ zoo') { alert("correct"); } else { alert("sorry, wrong"); } } else { alert("sorry, wrong"); } } else { //if person wrong dialog box pops says "sorry, wrong" alert("sorry, wrong"); } }
the error on line says if (secondanwser.touppercase() === 'febuary 14, 2005') {
the prompt() method returns input value if user clicks "ok". if user clicks "cancel" method returns null , script report error, because there no function on null object.
solution: check if answer isn't null, before call touppercase()
if (secondanswer != null && secondanwser.touppercase() === 'febuary 14, 2005')
Comments
Post a Comment