我的提示似乎不起作用

时间:2017-10-30 17:22:48

标签: javascript

我似乎无法让我的提示显示在此代码中,我必须为我的班级做一项任务。它应该显示最终的奖励积分数量,但是一旦我向我的JS添加其他提示和警报,就不会显示任何提示。有什么建议?

var numCoffees, awardPoints;

    numCoffees = prompt("How many coffees have you purchased?");
    if (numCoffees == 0)
      {awardPoints = 0;}

    if (numCoffees == 1)
      {awardPoints = 2;}

    if (numCoffees == 2)
      {awardPoints = 5;}

    if (numCoffees == 3)
      {awardPoints = 9;}

    if (numCoffees > 3)
      {awardPoints = ((9+2)*(numCoffees-3));}

  /*Determine Preferred Customer status*/

 var PreferredCustomer; 

    PreferredCustomer = prompt("Please say "yes" or "no" to indicate if you are a preferred customer.");
    if (PreferredCustomer == "yes")
      {awardPoints = awardPoints*2;}

  /*Display award points*/

    alert("awardPoints" + award points);

2 个答案:

答案 0 :(得分:2)

你的报价有问题。将第一个和最后一个引号更改为单引号:

PreferredCustomer = prompt('Please say "yes" or "no" to indicate if you are a preferred customer.');

或者,您可以转义内部引号:

PreferredCustomer = prompt("Please say \"yes\" or \"no\" to indicate if you are a preferred customer.");

并且,感谢kakamg0的评论,修复最后一行:

alert("awardPoints" + awardPoints);

答案 1 :(得分:0)

这里看起来主要有一些语法问题。逻辑上代码是写的。它的编写方式有点错误

正如其他人所指出的那样:引用错误

  • 在用双引号形成的字符串中使用双引号将在第一个双引号配对中结束字符串语句。

  • 警报框中的奖励点不是有效变量。它包含空格,未定义

总体上整理了一下代码,你最终得到了:

var numCoffees, awardPoints;

numCoffees = prompt("How many coffees have you purchased?");
if (numCoffees == 0) { awardPoints = 0; } else
if (numCoffees == 1) { awardPoints = 2; } else
if (numCoffees == 2) { awardPoints = 5; } else
if (numCoffees == 3) { awardPoints = 9; } else
if (numCoffees > 3) { awardPoints = ((9 + 2) * (numCoffees - 3));}

/*Determine Preferred Customer status*/

var PreferredCustomer;

PreferredCustomer = prompt("Please say 'yes'or 'no' to indicate if you are a preferred customer.");
if (PreferredCustomer == "yes") {
  awardPoints = awardPoints * 2;
}

/*Display award points*/

alert("awardPoints" + awardPoints);