Skip to content

Instantly share code, notes, and snippets.

@sethschori
Forked from anonymous/Shopping Calc (4.2).js
Created July 31, 2016 19:54
Show Gist options
  • Save sethschori/4b267cf25d93a5d719e3d7a163886f1c to your computer and use it in GitHub Desktop.
Save sethschori/4b267cf25d93a5d719e3d7a163886f1c to your computer and use it in GitHub Desktop.
https://repl.it/CQGL/89 created by sethopia
//Complete sumCart so that it will take the cart array and return the total cost for all
// the line items in the cart.
var cart = [
["tofu", {"quantity" : 3,"price" : 4.5} ],
["sriracha", {"quantity" : 1,"price" : 5} ],
["toilet paper", {"quantity" : 12,"price" : 1.75} ],
["Drano", {"quantity" : 1,"price" : 13} ],
["orichette", {"quantity" : 2,"price" : 7.5} ],
["hummus", {"quantity" : 2,"price" : 5.99} ],
["bison meat", {"quantity" : 3,"price" : 20.99} ],
["vegan bison meat", {"quantity" : 3,"price" : 24.99} ]
];
function sumCart(cart) {
var sum = 0;
for (var i = 0; i < cart.length; i++) {
var itemObj = cart[i][1]
sum += itemObj.quantity * itemObj.price;
}
return sum;
}
console.log(sumCart(cart));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment