出于某种原因,减去时的返回不正确

时间:2014-11-17 01:21:32

标签: javascript

这是我的代码。问题是减法呈现与变量noDiscount相同的回报。我曾尝试使用多种减法方法,但到目前为止还没有任何效果。另外我想知道是否有一种在java中编辑字体大小和字体类型的方法

// Variables 
var BREAK = "<br />"
var pricePound = 1.13;           // This is the price per pound
var amountPurchased = 0;         // Number of pounds purchased
var reduceRate = 0;              // This is the percent it is reduced
var noDiscount = pricePound * amountPurchased;     //Cost without discount
var amountSaved = reduceRate * noDiscount;
var reducCost = noDiscount - amountSaved;


 noDiscount = pricePound * amountPurchased
amountSaved = reduceRate * noDiscount
reducCost = noDiscount - amountSaved
document.write ("Hello and thank you for shopping at the ACME cement company. This program is here to handle transaction information and apply discounts. For all other concerns please go to our main site." + BREAK)
window.alert("Please be aware that all cement purchases are on sell and, the more you buy the more it will be discounted!!! All numbers inputted will be converted to pounds. Some restrictions and limitations do apply see a instore clerk for details.")
amountPurchased = parseFloat(prompt("Valued customer, please enter the amount of cement you would like to purchase." ));


parseFloat(amountPurchased)

if (amountPurchased <= 0) {
    window.alert("ERROR. Your purchase is below our minimum purchase amount. Please refresh the page and increase the amount you are trying to purchase." );
} else if (amountPurchased <= 500) {
    reduceRate = 0.02;
} else if (amountPurchased <= 9000) {
    reduceRate = 0.04;  
} else if (amountPurchased <= 15000) {
    reduceRate = 0.05;
} else { //only occurs when none of the others are true: so this is when it's >15000
    reduceRate = 0.09;
};
amountSaved = reduceRate * noDiscount
noDiscount = pricePound * amountPurchased
reducCost = noDiscount - amountSaved
document.write ("Your purchase of " + amountPurchased + "lbs of cement cost $" + noDiscount.toFixed(2) + ". It will be reduced by " +  reduceRate + "% bringing your total to $" + reducCost.toFixed(2));

</script>

2 个答案:

答案 0 :(得分:1)

在计算机程序中,命令的顺序很重要。在你的程序中 你首先从amountPurchased计算reducCost,然后你得到输入 来自用户并将其存储在amountPurchased中。

但是计算已经完成,而且reducCost不会被改变 这个输入。

答案 1 :(得分:0)

也许是这样的?我还将所有这些分成功能,并在点击按钮时执行此操作。

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Transaction Invoice</title>
    <meta name="author" content="Caprica" />
    <!-- Date: 2014-11-12 -->
</head>
<body>
<script type="text/javascript">
// Program name: Transaction Invoice
// Purpose: To do a transaction
// Author: Ephraim Vickers
// Date last modified: Today

// Variables 
var BREAK = "<br />"

var pricePound = 1.13;           // This is the price per pound
var amountPurchased = 0;         // Number of pounds purchased
var reduceRate = 0;              // This is the percent it is reduced
document.write ("Hello and thank you for shopping at the ACME cement company. This program is here to handle transaction information and apply discounts. For all other concerns please go to our main site." + BREAK)
window.alert("Please be aware that all cement purchases are on sell and, the more you buy the more it will be discounted!!! All numbers inputted will be converted to pounds. Some restrictions and limitations do apply see a instore clerk for details.")
amountPurchased = parseFloat(prompt("Valued customer, please enter the amount of cement you would like to purchase." ));

var noDiscount = pricePound * amountPurchased;     //Cost without discount
var amountSaved = reduceRate * noDiscount;
var reducCost = noDiscount - amountSaved;

parseFloat(amountPurchased)

if (amountPurchased <= 0) {
    window.alert("ERROR. Your purchase is below our minimum purchase amount. Please refresh the page and increase the amount you are trying to purchase." );
} else if (amountPurchased <= 500) {
    reduceRate = 0.02;
} else if (amountPurchased <= 9000) {
    reduceRate = 0.04;  
} else if (amountPurchased <= 15000) {
    reduceRate = 0.05;
} else { //only occurs when none of the others are true: so this is when it's >15000
    reduceRate = 0.09;
};

amountSaved = reduceRate * noDiscount
noDiscount = pricePound * amountPurchased
reducCost = noDiscount - amountSaved
document.write ("Your purchase of " + amountPurchased + "lbs of cement cost $" + noDiscount.toFixed(2) + ". It will be reduced by " +  reduceRate + "% bringing your total to $" + reducCost.toFixed(2));

</script>

所以这将是一个更好的解决方案:

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Transaction Invoice</title>
    <meta name="author" content="Caprica" />
    <!-- Date: 2014-11-12 -->

    <div>Hello and thank you for shopping at the ACME cement company. This program is here to handle transaction information and apply discounts. For all other concerns please go to our main site.</div>
    <div>Please be aware that all cement purchases are on sell and, the more you buy the more it will be discounted!!! All numbers inputted will be converted to pounds. Some restrictions and limitations do apply see a instore clerk for details.</div>
    <button onclick="Calculate();">New Order</button>
    <div id='msg'></div>

</head>

<body>
    <script type="text/javascript">
        // Program name: Transaction Invoice
        // Purpose: To do a transaction
        // Author: Ephraim Vickers
        // Date last modified: Today

        var pricePound = 1.13; // This is the price per pound
        var amountPurchased = 0; // Number of pounds purchased
        var reduceRate = 0; // This is the percent it is reduced

        var noDiscount = 0; //pricePound * amountPurchased;     //Cost without discount
        var amountSaved = 0; //reduceRate * noDiscount;
        var reducCost = 0; // noDiscount - amountSaved;

        function parseFloat(amountPurchased) {

            if (amountPurchased <= 0) {
                reduceRate = 0;
                //window.alert("ERROR. Your purchase is below our minimum purchase amount. Please refresh the page and increase the amount you are trying to purchase." );
            } else if (amountPurchased <= 500) {
                reduceRate = 0.02;
            } else if (amountPurchased <= 9000) {
                reduceRate = 0.04;
            } else if (amountPurchased <= 15000) {
                reduceRate = 0.05;
            } else { //only occurs when none of the others are true: so this is when it's >15000
                reduceRate = 0.09;
            }
            return;
        }


        function Calculate() {
            //set zeroes for all values
            amountPurchased = 0;
            reduceRate = 0;
            noDiscount = 0;
            amountSaved = 0;
            reducCost = 0;

            //get user input
            amountPurchased = prompt("Valued customer, please enter the amount of cement you would like to purchase.");
            parseFloat(amountPurchased);

            //calculate discount
            noDiscount = pricePound * amountPurchased; //Cost without discount
            amountSaved = reduceRate * noDiscount;
            reducCost = noDiscount - amountSaved;

            // output data
            if (reduceRate === 0) {
                document.getElementById('msg').innerHTML = "ERROR. Your purchase is below our minimum purchase amount. Please refresh the page and increase the amount you are trying to purchase.";
            } else {
                document.getElementById('msg').innerHTML = "Your purchase of " + amountPurchased + "lbs of cement cost $" + noDiscount.toFixed(2) + ". It will be reduced by " + reduceRate + "% bringing your total to $" + reducCost.toFixed(2)
            }

        }
    </script>