JSFiddle - React, Tailwind, and code Playground

by tonyjcamp

HTML

<div id="result"></div>

JavaScript

var basketModule = (function() {
    var basket = [];

    return {
        addItem: function(item) {
            basket.push(item);
        },
        getItemCount: function() {
            return basket.length;
        },
        getTotal: function() {
            var q = this.getItemCount(),
                x = 0,
                p = 0;
            
            // broken loop
            // for(; p < q; p++) {
                // p += backet[p].price;   
            // }
            
            // spicey loop
            for(; p < q; p++) {
                x += basket[p].price;
            }
            
            return x;
        }
    };

}());

// Just adding items to the array
basketModule.addItem({'name': 'BackPack', 'price': 300});
basketModule.addItem({'name': 'BackPack2', 'price': 200});

$('#result').text(basketModule.getTotal());