将参数传递给函数以保持运行总计

时间:2017-02-18 16:01:59

标签: javascript parameters arguments

我正在尝试通过将价格传递给函数来在Javascript中创建一个运行总计。

当用户按下按钮时,价格将传递到addBalance函数。我不确定如何添加和显示运行总计。

我被困住了。请帮忙。

// JavaScript Document
document.getElementById("computing").addEventListener('click', function() {
    "use strict";
    bookOrder("Computing");
}, false);
document.getElementById("english").addEventListener('click', function() {
    "use strict";
    bookOrder("English");
}, false);
document.getElementById("maths").addEventListener('click', function() {
    "use strict";
    bookOrder("Maths");
}, false);
document.getElementById("geography").addEventListener('click', function() {
    "use strict";
    bookOrder("Geography");
}, false);
document.getElementById("history").addEventListener('click', function() {
    "use strict";
    bookOrder("History");
}, false);
document.getElementById("science").addEventListener('click', function() {
    "use strict";
    bookOrder("Science");
}, false);
document.getElementById("showArray").addEventListener('click', showArrayValues);
var books = []; //array
//user defined function & passing parameters
function bookOrder(book) {
    "use strict";
    //conditional statement
    switch (book) {
        case "Computing":
            books.push("<p>Computing - £6.99</p>");
            addBalance(6.99);
            alert("Computing - £6.99, has been added to your order");
            break;
        case "English":
            books.push("<p>English - £7.99</p>");
            addBalance(7.99);
            alert("English - £7.99, has been added to your order");
            break;
        case "Maths":
            books.push("<p>Maths - £8.99</p>");
            addBalance(8.99);
            alert("Maths - £8.99, has been added to your order");
            break;
        case "Geography":
            books.push("<p>Geography - £9.99</p>");
            addBalance(9.99);
            alert("Geography - £9.99, has been added to your order");
            break;
        case "History":
            books.push("<p>History - £10.99</p>");
            addBalance(10.99);
            alert("History - £10.99, has been added to your order");
            break;
        case "Science":
            books.push("<p>Science - £11.99</p>");
            addBalance(11.99);
            alert("Science - £11.99, has been added to your order");
            break;
    }
}

function showArrayValues() {
    "use strict";
    document.getElementById("output").innerHTML = "";
    for (var i in books) //loop
    { //manipulating the DOM
        document.getElementById("output").innerHTML += books[i];
    }
}

function addBalance(param1) {
    "use strict";
    parseFloat(document.getElementById("total").innerHTML += param1);
}

2 个答案:

答案 0 :(得分:0)

在顶部添加:

var total=0;

function addBalance (param1){
    "use strict";   
     total+=param1;
    document.getElementById("total").innerHTML=total;
}

答案 1 :(得分:0)

您可以使用数组缩减轻松计算总数。 e.g。

var calc = total.reduce(function(sum, val) {
    return sum + val;
}, 0);

请参阅fiddle此处