简体   繁体   中英

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. 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]". 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)"

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) . 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.

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). 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'); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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