🎓 A1Learner
🔍
0 of 50 lessons completed

JavaScript Project: Shopping Cart

A shopping cart is a small program that holds a list of chosen items. A closure keeps the list private, and methods add items, remove items, and work out the total.

The factory and its hidden list

createCart builds a cart. The list lives inside the factory, and the returned methods share it.

function createCart() {
  const items = [];
  return {
    count: function () {
      return items.length;
    },
  };
}
const cart = createCart();
console.log(cart.count());

Add items

add pushes one item onto the hidden list. Each item holds a name and a price.

function createCart() {
  const items = [];
  return {
    add: function (name, price) {
      items.push({ name: name, price: price });
    },
    count: function () {
      return items.length;
    },
  };
}
const cart = createCart();
cart.add("Book", 12);
cart.add("Pen", 3);
console.log(cart.count());

Remove an item

remove finds the first item with the given name. It takes that item out of the list and leaves the rest.

function createCart() {
  const items = [];
  return {
    add: function (name, price) {
      items.push({ name: name, price: price });
    },
    remove: function (name) {
      const index = items.findIndex(function (item) {
        return item.name === name;
      });
      if (index !== -1) {
        items.splice(index, 1);
      }
    },
    count: function () {
      return items.length;
    },
  };
}
const cart = createCart();
cart.add("Book", 12);
cart.add("Pen", 3);
cart.remove("Pen");
console.log(cart.count());

Work out the total

total adds up the price of every item in the list.

function createCart() {
  const items = [];
  return {
    add: function (name, price) {
      items.push({ name: name, price: price });
    },
    total: function () {
      return items.reduce(function (sum, item) {
        return sum + item.price;
      }, 0);
    },
  };
}
const cart = createCart();
cart.add("Book", 12);
cart.add("Pen", 3);
console.log(cart.total());

Example

function createCart() {
  const items = [];
  return {
    add: function (name, price) {
      items.push({ name: name, price: price });
    },
    remove: function (name) {
      const index = items.findIndex(function (item) {
        return item.name === name;
      });
      if (index !== -1) {
        items.splice(index, 1);
      }
    },
    total: function () {
      return items.reduce(function (sum, item) {
        return sum + item.price;
      }, 0);
    },
    count: function () {
      return items.length;
    },
  };
}
const cart = createCart();
cart.add("Book", 12);
cart.add("Pen", 3);
cart.add("Book", 12);
cart.remove("Pen");
console.log("Items: " + cart.count());
console.log("Total: " + cart.total());
console.log(cart.items);

Key points

  • Closure: a function that keeps its surrounding variables after the outer function returns
  • Private state: data that only the returned methods can reach
  • add: a method that puts one item into the cart
  • remove: a method that takes one item out of the cart
  • total: a method that adds up the prices of the items in the cart
  • The cart list lives inside the factory function, so no code outside can change it directly.

In this example, createCart builds a cart with a private list of items. The program adds three items and removes the pen. The cart holds two books, so the count is 2 and the total is 24. The list itself stays hidden, so reading cart.items gives undefined.

Note

The methods share one list, so every method sees the same items.

Try it Yourself

Edit the code, then press Run.

Runs in your browser — the preview under the editor is the real result. Nothing is sent to a server. The code runner panel (right) does the same with more room.

Exercise

Where does the cart's list of items live?