在JavaScript中进行简单的运输和处理计算 [英] In JavaScript doing a simple shipping and handling calculation

查看:107
本文介绍了在JavaScript中进行简单的运输和处理计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用简单的JavaScript计算时遇到问题。如果订单价格为25美元或更低,我的文件应该为订单增加1.50美元,如果超过25美元,则增加订单的10%。确切的问题是:

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 $25. The exact problem is:

许多公司通常会收取运费和手续费。创建一个允许用户在文本框中输入购买价格的网页,其中包含一个计算运费和处理的JavaScript函数。为脚本添加功能,对于小于或等于$ 25.00的任何购买,增加1.50美元的最低运费和手续费。对于任何超过$ 25.00的订单,将运费和处理的总购买价格加10%,但不包括1.50美元的最低运费和手续费。计算百分比的公式是价格*百分比/ 100.例如,计算50.00美元购买价格10%的公式为50 * 10/100,这导致运费和手续费为5.00美元。确定订单的总成本(购买加运费和处理)后,将其显示在警告对话框中。

Many companies normally charge a shipping and handling charge for purchases. Create a Web page that allows a user to enter a purchase price into a text box and includes a JavaScript function that calculates shipping and handling. Add functionality to the script that adds a minimum shipping and handling charge of $1.50 for any purchase that is less than or equal to $25.00. For any orders over $25.00, add 10% to the total purchase price for shipping and handling, but do not include the $1.50 minimum shipping and handling charge. The formula for calculating a percentage is price * percent / 100. For example, the formula for calculating 10% of a $50.00 purchase price is 50 * 10 / 100, which results in a shipping and handling charge of $5.00. After you determine the total cost of the order (purchase plus shipping and handling), display it in an alert dialog box.

这是我的代码:

    var price = 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 / 100
}
}
window.alert("Your total is $" + total + ".");

测试时我在提示框中输入一个数字,而不是像我输入一个数字一样计算它计算好像我输入了一个字符串。即我输入19,它给我191.5或我输入26,它给我262.6

When testing I enter a number in the prompt box, and instead of calculating as if I entered a number it calculates as if I entered a string. i.e. i enter 19 and it gives me 191.5 or I enter 26 and it gives me 262.6

推荐答案

使用 parseFloat 将帮助您:

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

查看工作地点: http://jsfiddle.net/e8U6W/

此外,一个鲜为人知的更有效的做法是只需 -0

Also, a little-known put more performant way of doing this would be simply to -0:

var price =window.prompt("What is the purchase price?", 0) - 0;

(参见:消除零某种JavaScript性能技巧?

确保对此进行评论,尽管对于那些阅读代码的人来说并不那么明显 parseFloat

Be sure to comment this, though as its not as obvious to those reading your code as parseFloat

这篇关于在JavaScript中进行简单的运输和处理计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆