无法找到错误

时间:2016-04-02 18:01:30

标签: javascript

我无法理解为什么这不起作用,控制台并没有说出现错误,但是当它出现时#&#39? 34;提示"第二次它不起作用。 这是代码:

const Fanta = 250,Sprite = 250,Cola = 250,Dirol = 450,Snickers = 300,Lays = 800;
var a = prompt("1:Fanta = 250, 2:Sprite = 250, 3:Coca Cola = 250, 4:Dirol = 450, 5:Snickers = 300, 6:Lays = 800");
function math()
{
if(a == 1)
{
alert("You chose Fanta");
var money = prompt("Put money in");
alert("You put in " + money)
if(money >= 250)
{
    alert("You just purchased Fanta! Take back" + money - Fanta);
}
}
}

1 个答案:

答案 0 :(得分:5)

您有一些函数math()中包含的代码未被调用。一种解决方案是打开它(删除功能):

const Fanta = 250,
  Sprite = 250,
  Cola = 250,
  Dirol = 450,
  Snickers = 300,
  Lays = 800;
var a = prompt("1:Fanta = 250, 2:Sprite = 250, 3:Coca Cola = 250, 4:Dirol = 450, 5:Snickers = 300, 6:Lays = 800");


  if (a == 1) {
    alert("You chose Fanta");
    var money = prompt("Put money in");
    alert("You put in " + money)
    if (money >= 250) {
      alert("You just purchased Fanta! Take back" + money - Fanta);
    }
  }

另一个解决方案是调用函数math()

var a = prompt(...);
math();

您应在分配到a后调用它。当然,你的函数会使用a作为一个本质上的全局变量 - 这被认为是一种不好的做法 - 所以你可能想稍微重构一下你的代码以明确地将它传递给math()。 p>

相关问题