我试图创建一个程序,将数组var prices = [12.3, 20, 30.33];
的三个值加在一起,并输出总和和总帐单,税率为7%。
这是我的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:
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;
}
我做错了什么?
答案 0 :(得分:0)
你没有调用函数
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>
答案 1 :(得分:0)
您没有调用该函数,您需要将实际值与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>