2021-04-20

Apr 21 In-Class Exercise Thread .

Please post your solutions to the Apr 21 In-Class Exercise to this thread.

Best,

Chris

Please post your solutions to the Apr 21 In-Class Exercise to this thread. Best, Chris

-- Apr 21 In-Class Exercise Thread

<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]);
}

} &lt;/pre&gt;

(Edited: 2021-04-21)
<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>

-- Apr 21 In-Class Exercise Thread

&lt;pre&gt;

function Cart(theGroceryList) { this.groceryList = theGroceryList this.prettyPrint = function() { this.groceryList.forEach(groceryItem =&gt; { console.log(${groceryItem.item} costs ${groceryItem.price}) }); } }

function GroceryItem(theItem, thePrice) { this.item = theItem this.price = thePrice }

let groceryList = [ new GroceryItem(&#039;Milk&#039;, &#039;3.50'), new GroceryItem('Apples', '0.50&#039;), new GroceryItem(&#039;Chips&#039;, &#039;2.00'), new GroceryItem('Cereal', '2.99&#039;) ] let cart = new Cart(groceryList) cart.prettyPrint()

&lt;/pre&gt;

(Edited: 2021-04-21)
<pre> function Cart(theGroceryList) { this.groceryList = theGroceryList this.prettyPrint = function() { this.groceryList.forEach(groceryItem => { console.log(@BT@${groceryItem.item} costs ${groceryItem.price}@BT@) }); } } 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>

-- Apr 21 In-Class Exercise Thread

function Cart(price_list){

this.list = price_list;

Cart.prototype.prettyPrint = function(){ document.write(&quot;&lt;ul&gt;&quot;); this.list.forEach(item =&gt; document.write(&quot;&lt;li&gt;&quot; + item + &quot;&lt;/li&gt;&quot;)); document.write(&quot;&lt;/ul&gt;&quot;); } }

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>"); } }

-- Apr 21 In-Class Exercise Thread
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)
function cart(groceries){ this.groceries = groceries; cart.prototype.prettyPrint = () => { this.groceries.map((item) => { document.writeln(@BT@<p>item: ${item[0]} price:${item[1]}</p>@BT@); }) } } grocery_list = new cart([['eggs', 2], ['milk', 3], ['bread', 10]]); grocery_list.prettyPrint();

-- Apr 21 In-Class Exercise Thread

&lt;nowiki&gt; function cart(groceriesPrices){ this.groceries = groceriesPrices[0] this.prices = groceriesPrices[1] this.listPrices = function(){ for(var i = 0; i &lt; this.prices.length; i++){ console.log(this.groceries[i] +&quot; : &quot;+this.prices[i]) } } } var grocerylist = [[&quot;apple&quot;,&quot;orange&quot;,&quot;mango&quot;,&quot;banana&quot;],[1,3,6,3]] myCart = new cart(grocerylist); myCart.listPrices() &lt;/nowiki&gt;

(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>

-- Apr 21 In-Class Exercise Thread

function Cart ( procery, price) { this.procery = procery this.price = price var prettyPrint = console.log ( procery + &quot; is $&quot; + price)

} Cart (&quot;Table&quot;, 15)

Resource Description for Screen Shot 2021-04-21 at 4.10.46 PM.png

(Edited: 2021-04-21)
function Cart ( procery, price) { this.procery = procery this.price = price var prettyPrint = console.log ( procery + " is $" + price) } Cart ("Table", 15) ((resource:Screen Shot 2021-04-21 at 4.10.46 PM.png|Resource Description for Screen Shot 2021-04-21 at 4.10.46 PM.png))

-- Apr 21 In-Class Exercise Thread

&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;inclass exercise&lt;/title&gt; &lt;meta charset=&quot;utf-8&quot;/&gt; &lt;/head&gt; &lt;body&gt; &lt;script&gt; function prettyPrint() { for(i = 0; i&lt;this.groceries_prices[0].length; i++) { document.write(this.groceries_prices[0][i], &quot;: &quot;, this.groceries_prices[1][i], &quot;&lt;br /&gt;&quot;); } }

        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>

&lt;/html&gt;

<!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>

-- Apr 21 In-Class Exercise Thread

&lt;nowiki&gt; &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;inclass exercise&lt;/title&gt; &lt;meta charset=&quot;utf-8&quot;/&gt; &lt;/head&gt; &lt;body&gt; &lt;script&gt;

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 &lt; this.groceryList.length; i++) { document.write(this.groceryList[i] + this.priceList[i] + &quot;&lt;br/&gt;&quot;); } }

var grocery_prices = []; grocery_prices[&#039;eggs&#039;] = 2.00; grocery_prices[&#039;milk&#039;] = 3.00; grocery_prices[&#039;bread&#039;] = 4.00;

my_cart = cart(grocery_prices); my_cart.display();

&lt;/script&gt; &lt;/body&gt; &lt;/html&gt;&lt;/nowiki&gt;

(Edited: 2021-04-21)
<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>

-- Apr 21 In-Class Exercise Thread

&lt;!doctype html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt; test &lt;/title&gt; &lt;meta charset=&quot;utf-8&quot; /&gt;

&lt;/head&gt; &lt;body&gt; &lt;script&gt; function cart(groceries, prices) { this.groceries = groceries; this.prices = prices; cart.prototype.prettyPrint = function() { document.write(this.groceries, &quot;&lt;br /&gt;&quot;); document.write(this.prices, &quot;&lt;br /&gt;&quot;); }; }

var a = new cart(&quot;egg&quot;, &quot;$5&quot;); a.prettyPrint();

&lt;/script&gt; &lt;/body&gt; &lt;/html&gt;

<!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>
[ Next ]
X