简体   繁体   English

JavaScript函数不适用于整数

[英]JavaScript Function doesn't work properly with integers

I have a javascript function that is supposed to ask the user how many products they would like to order. 我有一个javascript函数,该函数应该询问用户他们要订购多少产品。 The function is supposed to give a message when they order less than one product. 当他们订购的产品少于一种时,该功能应该发出消息。 It is also supposed to send an alert saying "Ordering (quantity) (product)[s]". 还应该发送一条警告,说“订购(数量)(产品)[s]”。 These don't seem to be working properly. 这些似乎无法正常工作。

I've tried returning the quantity but that just seems to change the webpage to the quantity number. 我尝试返回数量,但似乎只是将网页更改为数量编号。 This does show that the quantity is working, however. 但是,这确实表明数量有效。

function promptQuantity(product) {
  var quantity = prompt("How many " + product + "s would you like?");
  if (quantity > 1) {
    var plural = "s";
  }
  if (quantity = 1) {
    var plural = "";
  }
  if (quantity < 1) {
    alert("Don't be ridiculous! You can't order less than one " + product + "!");
  }
  if (quantity > 0) {
    alert("Ordering " + quantity + " " + product, plural);
  }
}

I expect this function to send an alert to the user telling them that they have ordered quantity of product, however it just returns saying "Ordering 1 (product)" 我希望该功能向用户发送警报,告诉他们他们已经订购了一定数量的产品,但是它只是返回说“订购1(产品)”

The piece of code if (quantity = 1) is wrong, you are doing and assignment and quantity will be set to 1 , for comparison use if (quantity == 1) . 这段代码if (quantity = 1)错误,您正在做,并且分配和quantity将设置为1 ,以供if (quantity == 1)比较使用。 However, your code can be restructured like this: 但是,您的代码可以像这样重组:

 function promptQuantity(product) { var quantity = prompt("How many " + product + "s would you like?"); var plural = quantity > 1 ? "s" : ""; if (quantity < 1) alert("Don't be ridiculous! You can't order less than one " + product + "!"); else alert("Ordering " + quantity + " " + product + plural); } promptQuantity("Short"); 

First of all - you should use '==' instead of '=' to compare 'a' and 'b' for equality. 首先-您应该使用'=='而不是'='来比较'a'和'b'的相等性。

Also, there is no need to check for '==' or '<' if you already know that 'a' is greater then 'b', so it's better to use if-else construction (or even switch). 另外,如果您已经知道'a'大于'b',则无需检查'=='或'<',因此最好使用if-else构造(甚至切换)。 So it may be optimised as: 因此,可以将其优化为:

 function promptQuantity(product) { var quantity = prompt("How many " + product + "s would you like?"); var message = ''; if (quantity > 1) { message = "Ordering " + quantity + " " + product + "s"; } else if (quantity == 1) { message = "Ordering " + quantity + " " + product; } else { message = "Don't be ridiculous! You can't order less than one " + product + "!" } alert(message); } promptQuantity('apple'); 

And using switch, but it has less obvious action 和使用开关,但动作不太明显

 function promptQuantity(product) { var quantity = prompt("How many " + product + "s would you like?"); var message = ''; switch (true) { case quantity > 1: message = "Ordering " + quantity + " " + product + "s"; break; case quantity == 1: message = "Ordering " + quantity + " " + product; break; default: message = "Don't be ridiculous! You can't order less than one " + product + "!" break; } alert(message); } promptQuantity('apple'); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM