Please post your solutions to the Apr 21 In-Class Exercise to this thread.
Best,
Chris
<pre> function cart(grocery_prices) { this.grocery_prices = grocery_prices;
cart.prototype.prettyPrint = function() {
for(var i=0; i<this.grocery_prices.length; i++) {
document.write(this.grocery_prices[i]);
}
} </pre>
(Edited: 2021-04-21)<pre>
function Cart(theGroceryList)
{
this.groceryList = theGroceryList
this.prettyPrint = function() {
this.groceryList.forEach(groceryItem => {
console.log(${groceryItem.item} costs ${groceryItem.price})
});
}
}
function GroceryItem(theItem, thePrice) { this.item = theItem this.price = thePrice }
let groceryList = [
new GroceryItem('Milk', '3.50'),
new GroceryItem('Apples', '0.50'),
new GroceryItem('Chips', '2.00'),
new GroceryItem('Cereal', '2.99')
]
let cart = new Cart(groceryList)
cart.prettyPrint()
</pre>
(Edited: 2021-04-21)function Cart(price_list){
this.list = price_list;
Cart.prototype.prettyPrint = function(){ document.write("<ul>"); this.list.forEach(item => document.write("<li>" + item + "</li>")); document.write("</ul>"); } }
function cart(groceries){
this.groceries = groceries;
cart.prototype.prettyPrint = () => {
this.groceries.map((item) => {
document.writeln(`<p>item: ${item[0]} price:${item[1]}</p>`);
})
}
}
grocery_list = new cart([['eggs', 2], ['milk', 3], ['bread', 10]]);
grocery_list.prettyPrint();(Edited: 2021-04-21) <nowiki> function cart(groceriesPrices){ this.groceries = groceriesPrices[0] this.prices = groceriesPrices[1] this.listPrices = function(){ for(var i = 0; i < this.prices.length; i++){ console.log(this.groceries[i] +" : "+this.prices[i]) } } } var grocerylist = [["apple","orange","mango","banana"],[1,3,6,3]] myCart = new cart(grocerylist); myCart.listPrices() </nowiki>
(Edited: 2021-04-21)function Cart ( procery, price) { this.procery = procery this.price = price var prettyPrint = console.log ( procery + " is $" + price)
} Cart ("Table", 15)
(Edited: 2021-04-21)<!DOCTYPE html> <html> <head> <title>inclass exercise</title> <meta charset="utf-8"/> </head> <body> <script> function prettyPrint() { for(i = 0; i<this.groceries_prices[0].length; i++) { document.write(this.groceries_prices[0][i], ": ", this.groceries_prices[1][i], "<br />"); } }
function Cart(groceries_prices)
{
this.groceries_prices = groceries_prices;
this.prettyPrint = prettyPrint;
}
arr = [["Tomato","Potato","Apple","Banana"],["$3","$5","$1","$2"]]
var cart = new Cart(arr);
cart.prettyPrint();
</script>
</body>
</html>
<nowiki> <!DOCTYPE html> <html> <head> <title>inclass exercise</title> <meta charset="utf-8"/> </head> <body> <script>
function cart(grocery_prices) { for (var item in grocery_prices) { this.groceryList.concat(item); this.priceList.concat(grocery_prices[item]); } this.display = prettyPrint; }
function prettyPrint() { for (var i = 0; i < this.groceryList.length; i++) { document.write(this.groceryList[i] + this.priceList[i] + "<br/>"); } }
var grocery_prices = []; grocery_prices['eggs'] = 2.00; grocery_prices['milk'] = 3.00; grocery_prices['bread'] = 4.00;
my_cart = cart(grocery_prices); my_cart.display();
</script> </body> </html></nowiki>
(Edited: 2021-04-21)<!doctype html> <html> <head> <title> test </title> <meta charset="utf-8" />
</head> <body> <script> function cart(groceries, prices) { this.groceries = groceries; this.prices = prices; cart.prototype.prettyPrint = function() { document.write(this.groceries, "<br />"); document.write(this.prices, "<br />"); }; }
var a = new cart("egg", "$5"); a.prettyPrint();
</script> </body> </html>