In JavaScript doing a simple shipping and handling calculation

前端 未结 6 705
清歌不尽
清歌不尽 2021-01-14 23:39

I am having trouble with a simple JavaScript calculation. My document is supposed to add $1.50 to an order if it is $25 or less, or add 10% of the order if it is more then $

6条回答
  •  一生所求
    2021-01-14 23:48

    A bit of redundant multiplication, but your problem is that the numbers that are being inputted are treated as strings, not numbers. You have to convert them to floating point numbers:

    var price = parseFloat(window.prompt("What is the purchase price?", 0));
    var shipping = calculateShipping(price);
    var total = price + shipping;
    
    function calculateShipping(price)
    {
      if (price <= 25)
      {
       return 1.5;
      } else {
        return price / 10
      }
    }
    
    window.alert("Your total is $" + total + ".");
    

提交回复
热议问题