Created
May 2, 2015 05:30
-
-
Save bpownow/befa93f47a1f2384c81c to your computer and use it in GitHub Desktop.
Playing with shopping cart exercise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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;
}
}
};
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
};
};
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);