[英]Javascript Tax Calculator
I'm trying to create a program that sums the three values of the array var prices = [12.3, 20, 30.33];
我正在尝试创建一个程序,该程序求和数组var prices = [12.3, 20, 30.33];
的三个值var prices = [12.3, 20, 30.33];
together, and outputs both the sum and the total bill with tax of 7%. 一起输出总金额和总税额为7%的税。
Here is my HTML: 这是我的HTML:
<p>Sum of numbers in array: <span id="sum"></span></p>
<p>Amount with 7% tax added: <span id="percent"></span></p>
<button onclick="myFunction()">Click Me</button>
JS: JS:
var numbers = [12.3, 20, 30.33];
var taxAmount = .07;
function getSum(total, num) {
return total + num;
}
function myFunction(item) {
document.getElementById("sum").innerHTML = numbers.reduce(getSum);
}
function multiply(item) {
document.getElementById("percent").innerHTML = numbers.reduce(getSum) * .07;
}
What am I doing wrong? 我究竟做错了什么?
you were not calling the function 您没有调用该函数
var numbers = [12.3, 20, 30.33]; var taxAmount = .07; function getSum(total, num) { return total + num; } function myFunction(item) { document.getElementById("sum").innerHTML = numbers.reduce(getSum); document.getElementById("percent").innerHTML = numbers.reduce(getSum) * .07; }
<p>Sum of numbers in array: <span id="sum"></span></p> <p>Amount with 7% tax added: <span id="percent"></span></p> <button onclick="myFunction()">Click Me</button>
You don't called the funtion and you need to sum the actual value with the tax of 7%. 您没有调用该功能,您需要将实际值与7%的税费相加。
numbers.reduce(getSum) + numbers.reduce(getSum) * .07
var numbers = [12.3, 20, 30.33]; var taxAmount = .07; function getSum(total, num) { return total + num; } function myFunction(item) { document.getElementById("sum").innerHTML = numbers.reduce(getSum); document.getElementById("percent").innerHTML = numbers.reduce(getSum) + numbers.reduce(getSum) * .07; }
<p>Sum of numbers in array: <span id="sum"></span></p> <p>Amount with 7% tax added: <span id="percent"></span></p> <button onclick="myFunction()">Click Me</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.