开发者问题收集

无法读取 null 的属性“toUpperCase”(Google Chrome)

2014-11-19
6358

因此,我尝试运行一个简单的“条件语句练习”,其中包含几个问题,但在我的 Google Chrome 中,它一直给我一个错误。其他应用程序没有给我任何错误。我也有最新版本的 Google Chrome

警告,当您转到链接时,js 将启动

查看我的 js fiddle

var counter = 0;
var questions = 5;
var ready = false;

alert("I have " + questions + " questions to ask you?");

var name = prompt("What is your name?");
alert("Hello " + name + "!");

alert("Here we go!");

var answer1 = prompt(name + "What color is the sky?"); 
    if (answer1.toUpperCase() === 'BLUE') {
        counter += 1;
        alert('Congrates ' + name + ' you were right!');
    } else {
        alert('Sorry' + ' ' + name + 'but that was wrong.');
}
1个回答

我没有看到该错误。您可以尝试将该条件包装在值检查条件中:

var answer1 = prompt(name + "What color is the sky?"); 

if (answer1) {
    if (answer1.toUpperCase() === 'BLUE') {
        counter += 1;
        alert('Congrates ' + name + ' you were right!');
    } else {
        alert('Sorry' + ' ' + name + 'but that was wrong.');
    }
}
stewart715
2014-11-19