js中的数学运算?

时间:2018-11-16 21:32:03

标签: javascript

我正在尝试在JS中创建一个简单的更改计算器应用程序。我可以用我的美元和四分之一硬币找零钱,但在点钱时遇到了麻烦。这是到目前为止的代码。每次我尝试使硬币工作时,我都会遇到打印正确数量的麻烦。

var dollars, quarters, dimes, nickels;

function calculateChange() {
  var amountDue = document.getElementById('amount-due').value;
  var amountReceived = document.getElementById('amount-received').value;
  var change = amountReceived - amountDue;
  //document.getElementById('change-output').innerHTML = change.toFixed(2);

  dollars = Math.floor((change / 100) * 100);
  document.getElementById("dollars-output").innerHTML = dollars;
  var remainder = change - dollars;
  console.log(dollars)

  quarters = Math.floor((change % 1) * 4);
  document.getElementById('quarters-output').innerHTML = quarters;
  console.log(quarters)

  dimes = Math.floor((change % 15) * 0.10);
  document.getElementById('dimes-output').innerHTML = dimes;
  console.log(dimes)
  return;

  // = Math.floor((change % 0.1) * 20);
  //document.getElementById('nickels-output').innerHTML = nickels;

  // pennies =  Math.floor(Math.round(change % 0.05 * 100));
  //document.getElementById('pennies-output').innerHTML = pennies;

};

document.getElementById("calculate-change").addEventListener("click", calculateChange);
Amount due: <input id="amount-due"><br>
Amount-received: <input id="amount-received"><br>
<input type="button" value="Calculate change" id="calculate-change"><br>
Dollars: <span id="dollars-output"></span><br>
Quarters: <span id="quarters-output"></span><br>
Dimes: <span id="dimes-output"></span>

有人可以帮我看看该怎么做吗?

5 个答案:

答案 0 :(得分:2)

(change / 100) * 100change相同(除非存在浮点舍入错误),因此Math.floor((change / 100) * 100)Math.floor(change)相同。您应该只计算部门的底线,例如

dollars = Math.floor(change/100);

然后将其乘以100,然后从change中减去,以得到需要用较小面额处理的余数。对于每种面额,都用该硬币的价值除以,得到该硬币的底数,然后减去以得到下一个余数。

var dollars, quarters, dimes, nickels;

function calculateChange() {
  var amountDue = document.getElementById('amount-due').value;
  var amountReceived = document.getElementById('amount-received').value;
  var change = amountReceived - amountDue;
  //document.getElementById('change-output').innerHTML = change.toFixed(2);

  var dollars = Math.floor(change / 100);
  document.getElementById("dollars-output").innerHTML = dollars;
  var remainder = change - dollars * 100;

  var quarters = Math.floor(remainder / 25);
  document.getElementById('quarters-output').innerHTML = quarters;
  remainder = remainder - quarters * 25;

  var dimes = Math.floor(remainder / 10);
  document.getElementById('dimes-output').innerHTML = dimes;
  remainder = remainder - dimes * 10;

  var nickels = Math.floor(remainder/5);
  document.getElementById('nickels-output').innerHTML = nickels;
  remainder = remainder - nickels * 5;

  var pennies = remainder;
  document.getElementById('pennies-output').innerHTML = pennies;

};

document.getElementById("calculate-change").addEventListener("click", calculateChange);
Amount due: <input id="amount-due"><br>
Amount-received: <input id="amount-received"><br>
<input type="button" value="Calculate change" id="calculate-change"><br>
Dollars: <span id="dollars-output"></span><br>
Quarters: <span id="quarters-output"></span><br>
Dimes: <span id="dimes-output"></span><br>
Nickels: <span id="nickels-output"></span><br>
Pennies: <span id="pennies-output"></span><br>

答案 1 :(得分:1)

如果您使用数据结构来发挥自己的优势,则可以使代码更易于阅读(和缩短)。例如,而不是使用美元,四分之一等变量。您可以使用一个数组,该数组包含每个便士的价值。

如果执行此操作,您将能够利用javascript提供的工具使您的生活更轻松。另外,在处理货币时,通常会处理几美分的整数值(一美元= 100,四分之一= 25等),这会减少舍入误差。

如果您将这些值组成的数组表示面额,例如:

let coins = [100, 25, 10, 5, 1]

然后,您可以简单地用map()遍历列表,它将返回一个新列表,代表您需要多少次妖魔化:

let due = 1.51
let recieved = 3.00
let change = recieved * 100 - due * 100 // convert to pennies to avoid rounding

let coins = [100, 25, 10, 5, 1]         // denominations -- easy to adjust

let change_coins = coins.map(coin => {
    let n = Math.floor(change / coin)
    change -= n * coin
    return n
})

console.log(change_coins)
// 1 dollar, 1 quarter, 2 dimes, 0 nickels, 4 pennies

现在,如果您想添加包含2美元钞票(或5美元,数十美元等)的功能,只需在列表的开头添加200。

答案 2 :(得分:0)

您的计算似乎有些混乱。例如,(change / 100) * 100仅表示change。同样,(change % 1)仅表示change。您可能真正想要的是更多类似的东西:

dollars = Math.floor(change);
var remainderafterdollars = change - dollars;
quarters = Math.floor(remainderafterdollars / 0.25);
var remainderafterquarters = remainderafterdollars - (quarters * 0.25);
dimes = Math.floor(remainderafterquarters / 0.10);
var remainderafterdimes = remainderafterquarters - (dimes * 0.10);
nickels = Math.floor(remainderafterdimes / 0.05);
pennies = remainderafterdimes - (nickels * 0.05);

答案 3 :(得分:0)

我认为,没有%且仅进行简单划分就可以轻松得多。这样的事情会起作用:

const calcChange = function(amt) {
  const numDollars = Math.floor(amt);
  let remainder = amt - numDollars;
  const numQuarters = Math.floor(remainder / 0.25);
  remainder -= numQuarters * 0.25;
  const numDimes = Math.floor(remainder / 0.1);
  remainder -= numDimes * 0.1;
  const numNickles = Math.floor(remainder / 0.05);
  remainder -= numNickles * 0.05;
  const numPennies = Math.floor(remainder * 100);

  console.log(
    `Your change is ${numDollars} dollars, ${numQuarters} quarters, ${numDimes} dimes, ${numNickles}, and ${numPennies} pennies`
  );
};

答案 4 :(得分:-1)

让我们假设欠款126.35并收到140。

因此,13美元,2季度,1美分和5美分。

我建议您在将一个变量分开后,每次都减少更改字段。

change = change - dollars;
...

change = change - (quarters * 0.25);
...

以此类推。

这将使计算变得简单。