3
(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?

5
  • That depends on the IDE. It works jsfiddle.net/L364hf7v Commented Jan 11, 2016 at 19:19
  • What IDE are you using? Commented Jan 11, 2016 at 19:22
  • prompt is a window property function. If window is not in the context of a browser, as @adeneo said, prompt cannot exist. Commented Jan 11, 2016 at 19:24
  • @SterlingArcher My IDE is webstorm, and I also used window, and it still didn't work. Commented Jan 11, 2016 at 19:41
  • if you write window.prompt(), it should be recognized. Commented Jan 11, 2016 at 19:56

2 Answers 2

3

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 :)

Sign up to request clarification or add additional context in comments.

Comments

0

Open your terminal or command prompt.

To clear the screen, type the command clear and press Enter.

Initialize a new npm project by running the command npm init -y in your terminal or command prompt. This will create a package.json file with default values.

Install the prompt-sync package by running the command npm install prompt-sync in your terminal or command 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.