无法读取 null 的属性“toUpperCase”(谷歌浏览器)

Cannot read property 'toUpperCase' of null (Google Chrome)

本文关键字:toUpperCase 谷歌浏览器 属性 读取 null      更新时间:2023-09-26

所以我正在尝试运行一个简单的"条件语句练习",有几个问题,但在我的谷歌浏览器中,它一直给我一个错误。其他应用程序没有给我任何错误。我也有最新版本的谷歌浏览器

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

看看我的js小提琴

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.');
}

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

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.');
    }
}