Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

javascript "prompt is not defined" Reference Error

(function() {
    var random_num = Math.floor(Math.random()*10) + 1;
    var input_num = prompt("Guess the number between 1 and 10");
    if (input_num < 1 || input_num > 10) {
        alert('the input number is not between 1 and 10');

    } else if (random_num == input_num) {
        alert('Good Work');
    } else {
        alert('Not matched, the random number is: ' + random_num);
    }

})();

But the IDE displays that "prompt" is not defined. Could someone help me explain why?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Assuming you're using JSHint in WebStorm, set Preferences > Languages & Frameworks > JavaScript > Code Quality Tools > JSHint > Environments > Browser to true

Screenshot of WebStorm settings

Update: Also, as mentioned earlier, for good code you should use window.prompt, just so it's clear you're using the built in browser prompt rather than a custom function etc... (the same goes for alert, so use window.alert)

Finally, unrelated to the question, but something I noticed - it's good practice to use the === operator. There are a million articles online explaining why :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...