Post your solutions to the Nov 4 In-Class Exercise to this thread.
Best, Chris
(Edited: 2020-11-03)<nowiki><!DOCTYPE html> <html> <head> <title>Cart Test</title> </head> <body> <script> var Cart = new Object(); Cart.broccoli = 50; Cart.cabbage = 20; Cart.prettyPrint = function() { for(item in this) { if(item !== 'prettyPrint') { document.write(''+item+': '+Cart[item] + '<br />'); } } } Cart.prettyPrint() </script>
</body> </html></nowiki>
(Edited: 2020-11-04)Cart = { banana: "1.99", apple: "1.00", bread: "3.99", jelly: "4.99", prettyPrint: function() { for (prop in Cart){ if (prop != "prettyPrint"){ console.log(prop + ": " + Cart[prop]); }
}
}
}
Cart.prettyPrint();
function cart(){
this.items = new Array("cookies","cereal", "pancake mix");
this.prices = new Array(1.25,2.50,3.75);
cart.prototype.display = function (){
for(i = 0; i < this.items.length;i++){
if(this.items[i] != "prettyPrint"){
console.log("Item: ", this.items[i]);
console.log("Price: ",this.prices[i]);
}
}
}
}
my_cart = new cart(); my_cart.display();
<html>
<body> <script> function cart() { this.beans = "3.50", this.leeks = "4.44"; this.prettyPrint = function() { for (stuff in this) { if (stuff != "prettyPrint"){ document.write('<h1>' + stuff + ' ' + this[stuff] + '</h1>'); } } } }
var myCart = new cart();
myCart.prettyPrint();
</script> </body>
</html>
<nowiki> function display_cart() { console.log("Grocery Items and Prices: ", this.grocery_item," - ",this.price); }
function cart(new_grocery_item, new_price){
this.grocery_item = new_grocery_item;
this.price = new_price;
}
cart.prototype.display = display_cart; first_cart = new cart("cabbage", 1.50); second_cart = new cart("carrots", 0.99); first_cart.display(); second_cart.display(); </nowiki>
function cart(items, price)
{
this.items = items;
this.price = price;
}
function prettyPrint()
{
groceryCart = new cart(["milk", "cheese"], [2, 3]);
for (i = 0; i < groceryCart.items.length; i++)
{
console.log("Item: " + groceryCart.items[i] + " Price: $" + groceryCart.price[i]);
}
}
prettyPrint();
<body> <script type="text/javascript"> var shoppingCart = new Object(); shoppingCart.ice_cream = 7; shoppingCart.green_tea = 10; shoppingCart.prettyPrint = function() { for(merch in shoppingCart) { if(merch != "prettyPrint") { document.write(merch +''+ shoppingCart[merch] + '<br></br>'); } } } var cart = new shoppingCart(); cart.prettyPrint(); </script> </body>
function Cart(...items) { return{ prettyPrint:()=>{ for(item of items){ console.log("Name: ",item[0]," Price: ",item[1]) } }}
} var x=Cart(["milk",2],["banana",4],["apple",5]) x.prettyPrint()
var groceryList = new Object();
groceryList.Apple = "20";
groceryList.Orange = "25";
groceryList.Banana = "$19";
for (var prop in groceryList) { console.log("Food: ", prop, " Price: ", groceryList[prop]); }