(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?
(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?
Share Improve this question edited Jan 11, 2016 at 20:21 ssc-hrep3 16.1k8 gold badges51 silver badges96 bronze badges asked Jan 11, 2016 at 19:18 Alec_NAlec_N 311 gold badge1 silver badge3 bronze badges 5- That depends on the IDE. It works jsfiddle/L364hf7v – Rayon Commented Jan 11, 2016 at 19:19
- What IDE are you using? – MrOnlineCoder Commented Jan 11, 2016 at 19:22
-
prompt is a
window
property function. Ifwindow
is not in the context of a browser, as @adeneo said,prompt
cannot exist. – Sterling Archer Commented Jan 11, 2016 at 19:24 - @SterlingArcher My IDE is webstorm, and I also used window, and it still didn't work. – Alec_N Commented Jan 11, 2016 at 19:41
-
if you write
window.prompt()
, it should be recognized. – ssc-hrep3 Commented Jan 11, 2016 at 19:56
2 Answers
Reset to default 3Assuming 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 :)
Open your terminal or mand prompt.
To clear the screen, type the mand clear
and press Enter.
Initialize a new npm project by running the mand npm init -y
in your terminal or mand prompt. This will create a package.json
file with default values.
Install the prompt-sync
package by running the mand npm install prompt-sync
in your terminal or mand prompt. This package allows synchronous user input.
At the top of your code file, add the following line to import the prompt-sync
module and create a prompt object:
const prompt = require("prompt-sync")({ sigint: true });
Save your code file.
Run your code and check if the prompt functionality is working correctly.