Skip to content

Instantly share code, notes, and snippets.

@bpownow
Created May 2, 2015 05:30
Show Gist options
  • Save bpownow/befa93f47a1f2384c81c to your computer and use it in GitHub Desktop.
Save bpownow/befa93f47a1f2384c81c to your computer and use it in GitHub Desktop.
Playing with shopping cart exercise
var cart = {
items: [],
promoCode: null
};
var add_item_to_cart = function(item, quantity) {
for (var i=0; i<quantity; i++) {
cart.items.push(item);
}
};
console.log(cart);
var myItem = {
id: 1,
name: "Cool Thing!",
shortDesc: "This thing is a thing that does stuff!"
};
add_item_to_cart(myItem, 1);
add_item_to_cart(myItem, 2);
console.log(cart);
@bpownow
Copy link
Author

bpownow commented May 2, 2015

var cart = {
items: [],
promoCode: null
};

var is_item_in_cart = function(item) {
for (var i=0; i<cart.items.length; i++) {
if (cart.items[i].id == item.id) {
return true;
}
}

return false;

};

var add_item_to_cart = function(item, quantity) {
for (var i=0; i<quantity; i++) {
cart.items.push(item);
}
};

var add_item_to_cart_2 = function(item, quantity) {
var cartItem = {
item: item,
quantity: quantity
};

cart.items.push(cartItem);

};

var myItem = {
id: 1,
name: "Cool Thing!",
shortDesc: "This thing is a thing that does stuff!"
};

add_item_to_cart(myItem, 1);
add_item_to_cart(myItem, 2);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment