我编写了简单的重量转换代码,并编写了一个年度利润计算器程序,要求用户输入。当我在vscode中运行程序时,它显示错误“ ReferenceError:提示未定义。”
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)) ;
答案 0 :(得分:1)
prompt
在Node.js运行时中不是有效的构造(假定这是您通过VSCode运行的构造)。它将仅在基于浏览器的JavaScript引擎中工作。
有些库提供了类似的功能,但是您必须通过NPM安装并将其导入到脚本中。
答案 1 :(得分:0)
在使用Electron环境的VS Code中,prompt
函数不受支持,因为它是UI阻塞的。有关更多信息,请参见此issue。
您将需要在浏览器(例如下面的代码段)中运行示例。
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));