JSFiddle - React, Tailwind, and code Playground

by suresh veguru

JavaScript

var shoppingCart = (
    function () {
          var items = [];
       
        return {
            add: function(item)
            {
                items.push(item);
            },
        
            remove: function()
            {
                items.pop();
            },
            
            list: function()
            {
              return items;
            },
            price: function () 
            {
                return items.reduce(function (acc, item){
                return acc+item.price;
                },0);
            }
         };
    }()
);


alert(shoppingCart);
shoppingCart.add({name:"123",price:100});
shoppingCart.add({name:"234",price:200});
alert(shoppingCart.list());
alert(shoppingCart.price());