我的代码似乎正确,但我不知道为什么会收到此错误:
未捕获的TypeError:无法读取null的属性“ toUpperCase”
这是我的代码:
//The function is executed after someone clicks the "Take the Quiz"
function startquiz() {
//The variable for the first question
var FirstAnwser = prompt("Who posted the first youtube video?");
//The if statement for the first question
if (FirstAnwser.toUpperCase() === 'JAWED KARIM') {
//If the person is correct a dialog box that says correct pops up
alert("Correct");
//The Variable for the second question
var SecondAnwser = prompt("When was the domain name youtube.com activated?");
if (SecondAnwser.toUpperCase() === 'FEBUARY 14, 2005') {
alert("Correct");
var ThirdAnwser = prompt("What was the first video on youtube called?");
if (ThirdAnwser.toUpperCase() === 'ME AT THE ZOO') {
alert("Correct");
} else {
alert("Sorry, That is Wrong");
}
} else {
alert("Sorry, That is Wrong");
}
} else {
//If the person is wrong a dialog box pops up which says "Sorry, That is wrong"
alert("Sorry, That is Wrong");
}
}
Run Code Online (Sandbox Code Playgroud)
错误是在说 if (SecondAnwser.toUpperCase() === 'FEBUARY 14, 2005') {
如果用户单击“确定”,则hint()方法将返回输入值。如果用户单击“取消”,则该方法返回null,并且您的脚本报告错误,因为null对象上没有任何功能。
解决方案:致电前检查答案是否为空 toUpperCase()
if (SecondAnswer != null && SecondAnwser.toUpperCase() === 'FEBUARY 14, 2005')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3255 次 |
| 最近记录: |