2020-11-03

Nov 4 In-Class Exercise Thread .

Post your solutions to the Nov 4 In-Class Exercise to this thread.

Best, Chris

(Edited: 2020-11-03)
Post your solutions to the Nov 4 In-Class Exercise to this thread. Best, Chris
2020-11-04

-- Nov 4 In-Class Exercise Thread

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

-- Nov 4 In-Class Exercise Thread

Cart = { banana: &quot;1.99&quot;, apple: &quot;1.00&quot;, bread: &quot;3.99&quot;, jelly: &quot;4.99&quot;, prettyPrint: function() { for (prop in Cart){ if (prop != &quot;prettyPrint&quot;){ console.log(prop + &quot;: &quot; + Cart[prop]); }

    }
}

}

Cart.prettyPrint();

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

-- Nov 4 In-Class Exercise Thread

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

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

-- Nov 4 In-Class Exercise Thread

&lt;html&gt;

&lt;body&gt; &lt;script&gt; function cart() { this.beans = &quot;3.50&quot;, this.leeks = &quot;4.44&quot;; this.prettyPrint = function() { for (stuff in this) { if (stuff != &quot;prettyPrint&quot;){ document.write(&#039;&lt;h1&gt;&#039; + stuff + &#039; &#039; + this[stuff] + &#039;&lt;/h1&gt;&#039;); } } } }

var myCart = new cart();
myCart.prettyPrint();

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

&lt;/html&gt;

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

-- Nov 4 In-Class Exercise Thread

&lt;nowiki&gt; function display_cart() { console.log(&quot;Grocery Items and Prices: &quot;, this.grocery_item,&quot; - &quot;,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(&quot;cabbage&quot;, 1.50); second_cart = new cart(&quot;carrots&quot;, 0.99); first_cart.display(); second_cart.display(); &lt;/nowiki&gt;

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

-- Nov 4 In-Class Exercise Thread

function cart(items, price) {
this.items = items; this.price = price; }

function prettyPrint() { groceryCart = new cart([&quot;milk&quot;, &quot;cheese&quot;], [2, 3]); for (i = 0; i &lt; groceryCart.items.length; i++) { console.log(&quot;Item: &quot; + groceryCart.items[i] + &quot; Price: $&quot; + groceryCart.price[i]); }
} prettyPrint();

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

-- Nov 4 In-Class Exercise Thread

&lt;body&gt; &lt;script type=&quot;text/javascript&quot;&gt; var shoppingCart = new Object(); shoppingCart.ice_cream = 7; shoppingCart.green_tea = 10; shoppingCart.prettyPrint = function() { for(merch in shoppingCart) { if(merch != &quot;prettyPrint&quot;) { document.write(merch +&#039;&#039;+ shoppingCart[merch] + &#039;&lt;br&gt;&lt;/br&gt;&#039;); } } } var cart = new shoppingCart(); cart.prettyPrint(); &lt;/script&gt; &lt;/body&gt;

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

-- Nov 4 In-Class Exercise Thread

function Cart(...items) { return{ prettyPrint:()=&gt;{ for(item of items){ console.log(&quot;Name: &quot;,item[0],&quot; Price: &quot;,item[1]) } }}

} var x=Cart([&quot;milk&quot;,2],[&quot;banana&quot;,4],[&quot;apple&quot;,5]) x.prettyPrint()

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

-- Nov 4 In-Class Exercise Thread

var groceryList = new Object(); groceryList.Apple = &quot;20"; groceryList.Orange = "25&quot;; groceryList.Banana = &quot;$19&quot;;

for (var prop in groceryList) { console.log(&quot;Food: &quot;, prop, &quot; Price: &quot;, groceryList[prop]); }

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