Skip to content

Instantly share code, notes, and snippets.

@hassanuos
Forked from alexhawkins/shoppingCart.js
Created September 7, 2022 08:29
Show Gist options
  • Select an option

  • Save hassanuos/33ba80810aa851244f35f1b50bbbc1f3 to your computer and use it in GitHub Desktop.

Select an option

Save hassanuos/33ba80810aa851244f35f1b50bbbc1f3 to your computer and use it in GitHub Desktop.

Revisions

  1. @alexhawkins alexhawkins revised this gist Oct 10, 2014. 1 changed file with 6 additions and 5 deletions.
    11 changes: 6 additions & 5 deletions shoppingCart.js
    Original file line number Diff line number Diff line change
    @@ -46,6 +46,7 @@ var shoppingCart = (function() {
    }

    }

    }());

    shoppingCart.addItem({
    @@ -121,9 +122,9 @@ shoppingCart.removeItem('Samsung Galaxy');
    shoppingCart.getCart();
    /*
    Pink T-shirt $10.99
    AquaFresh $43.98
    Apple Ipad $750.99
    Eloquent JS $24.98
    Trojan Condoms $8.99
    Tube Sock $1.99
    AquaFresh $43.98
    Apple Ipad $750.99
    Eloquent JS $24.98
    Trojan Condoms $8.99
    Tube Sock $1.99
    */
  2. @alexhawkins alexhawkins created this gist Oct 10, 2014.
    129 changes: 129 additions & 0 deletions shoppingCart.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,129 @@
    /*MODULE PATTERN*/

    //create a namespace
    var shoppingCart = (function() {

    //private variables
    var total;
    var basket = [];

    //private methods
    function viewCart() {
    basket.forEach(function(el) {
    console.log(el.item + '\t\t$' + el.price);
    })
    };

    //public variables and methods
    return {
    //public alias to private function
    getCart: viewCart,

    addItem: function(values) {
    basket.push(values);
    return this;
    },

    removeItem: function(name) {
    basket.forEach(function(product, index) {
    if (name === product.item) {
    basket.splice(index, 1);
    }
    });
    return this;
    },

    getItemCount: function() {
    return basket.length;
    },

    getTotal: function() {
    total = 0;
    basket.forEach(function(item) {
    total += item.price;
    })
    return total;
    }

    }
    }());

    shoppingCart.addItem({
    item: 'Bobby Socks',
    price: 1.99
    }).addItem({
    item: 'Pink T-shirt',
    price: 10.99
    }).addItem({
    item: 'AquaFresh',
    price: 43.98
    }).addItem({
    item: 'Visine Ultra',
    price: 4.99
    }).addItem({
    item: 'Bobby Socks',
    price: 1.99
    });

    shoppingCart.getCart();
    /*
    Bobby Socks $1.99
    Pink T-shirt $10.99
    AquaFresh $43.98
    Visine Ultra $4.99
    Bobby Socks $1.99
    */
    console.log((shoppingCart.getTotal()).toFixed(2)); //63.94
    shoppingCart.removeItem('Visine Ultra'); //63.94
    console.log((shoppingCart.getTotal()).toFixed(2)); //58.95
    console.log(shoppingCart.removeItem('Bobby Socks').getTotal()); //54.97
    console.log(shoppingCart.getItemCount()); // 2

    shoppingCart.getCart();
    /*
    Pink T-shirt $10.99
    AquaFresh $43.98
    */

    console.log((shoppingCart.getTotal()).toFixed(2)); //54.97

    shoppingCart.addItem({
    item: 'Samsung Galaxy',
    price: 499.99
    }).addItem({
    item: 'Apple Ipad',
    price: 750.99
    }).addItem({
    item: 'Eloquent JS',
    price: 24.98
    }).addItem({
    item: 'Trojan Condoms',
    price: 8.99
    }).addItem({
    item: 'Tube Sock',
    price: 1.99
    });

    shoppingCart.getCart();

    /*
    Pink T-shirt $10.99
    AquaFresh $43.98
    Samsung Galaxy $499.99
    Apple Ipad $750.99
    Eloquent JS $24.98
    Trojan Condoms $8.99
    Tube Sock $1.99
    */

    console.log((shoppingCart.getTotal()).toFixed(2)); //1341.91
    shoppingCart.removeItem('Samsung Galaxy');
    shoppingCart.getCart();
    /*
    Pink T-shirt $10.99
    AquaFresh $43.98
    Apple Ipad $750.99
    Eloquent JS $24.98
    Trojan Condoms $8.99
    Tube Sock $1.99
    */