1

I wrote code for simple weight conversion and an annual profit calculator program asking the user for input. When I run the programs in vscode it displays the error "ReferenceError: prompt is not defined."

var weight_in_lbs = prompt("Please enter the weight in lbs you wish to convert:");
var pound_to_kg = (weight_in_lbs * .45359237);
console.log("The weight converted in kg is" + " " + pound_to_kg.toFixed(3))`
var annualSales = prompt("What is the projected total sales amount?");
var salesPrediction = (annualSales * .23);
console.log ("Your annual profit is " + salesPrediction.toFixed(2)) ;
4
  • Are you running it in a Node env by any chance? Commented Aug 31, 2019 at 18:19
  • @Clarity the OP says it's being run in vscode Commented Aug 31, 2019 at 18:21
  • @Pointy that does sound like it's run in the node env then. Unless I'm missing smth. Commented Aug 31, 2019 at 18:25
  • 1
    kindly give few more details to proceed giving suggestion Commented Aug 31, 2019 at 18:27

2 Answers 2

2

prompt is not a valid construct in the Node.js runtime (which assumedly is what you are running in through VSCode). It will only work in browser-based JavaScript engines.

There are libraries that offer similar functionality, but you would have to install via NPM and import them into your scripts.

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

Comments

0

In VS Code, which uses the Electron environment, the prompt function is not support as it is UI blocking. See this issue for more info.

You will need to run your example in a browser such as in the snippet below.

var weight_in_lbs = prompt("Please enter the weight in lbs you wish to convert:");
var pound_to_kg = (weight_in_lbs * .45359237);
console.log("The weight converted in kg is" + " " + pound_to_kg.toFixed(3))
var annualSales = prompt("What is the projected total sales amount?");
var salesPrediction = (annualSales * .23);
console.log("Your annual profit is " + salesPrediction.toFixed(2));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.