最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot read property 'toUpperCase' of null (Google Chrome) - Stack Overflow

programmeradmin0浏览0评论

So I'm trying to run a simple "conditional statement exercise" with a few questions, but in my Google Chrome it keeps giving me an error. Other applications aren't giving me any errors. I also have the most updated version of Google Chrome

Warning the js is going to launch when you go to the link

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

So I'm trying to run a simple "conditional statement exercise" with a few questions, but in my Google Chrome it keeps giving me an error. Other applications aren't giving me any errors. I also have the most updated version of Google Chrome

Warning the js is going to launch when you go to the link

Check out my 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.');
}
Share Improve this question asked Nov 19, 2014 at 2:05 EliCollinsEliCollins 891 silver badge8 bronze badges 3
  • Is working ok for me in chrome38 – ianaya89 Commented Nov 19, 2014 at 2:07
  • Cannot read property 'toUpperCase' of null - do not press Cancel button. Or assign the return of prompt to variable and check if it is a boolean or a string before applying toUpperCase function - only strings have it. – Cheery Commented Nov 19, 2014 at 2:11
  • When I launched the "app" I didn't press cancel but I guess I have to to wrap the conditional in a conditional to get to fully work on a my version of chrome??? – EliCollins Commented Nov 19, 2014 at 2:41
Add a ment  | 

1 Answer 1

Reset to default 5

I'm not seeing that error. You can try wrapping that conditional in a value check conditional:

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.');
    }
}
发布评论

评论列表(0)

  1. 暂无评论