// Constructor to create an instnace of a shopper object
function Shopper(givenName, familyName, email) {
this.givenName = givenName;
this.familyName = familyName;
this.email = email;
this.cart = [ ];
// Method to add items to the shopping cart
this.addToCart = function (theItem) {
this.cart.push(theItem);
}
// Method to reset the shoppting cart
this.reset = function() {
this.cart = [ ];
}
// Calculate the total cost of items in the shopping cart
this.totalSpend = function () {
var total = 0.00;
for (var i=0 ; i< this.cart.length; i++) {
total += this.cart[i].cost;
}
return (total);
}
// Return a string that identifies the shopper
this.name = function () {
var who = this.givenName + " " + this.familyName;
return who;
}
}
// Constructor to create an object containing an item for sale
function Item(description, cost, image) {
this.description = description;
this.cost = cost;
this.image= image;
}
// An array of item objects available for puchase in the store
var imageDir = "http://www.bvonkonsky.com/images/bwt/";
var theStore = [
{description: "chair", cost:100.0, image:"chair.jpg"},
{description: "table", cost:1500.0, image:"table.jpg"},
{description: "lamp", cost:50.00, image:"lamp.jpg"},
];
/*
var theStore = [
new Item ("Chair", 100.00, "chair.jpg"),
new Item ("Table", 1500.00, "table.jpg"),
new Item ("Lamp", 50.00, "lamp.jpg")
];
*/
// Get the store elemeent from the DOM
var storeElement = document.getElementById("store");
// Iterate through each item in the store, creating a tumbnail view for each
for (var i=0 ; i<theStore.length ; i++) {
// Create a new div for the thumbnail for this item
var divElement = document.createElement("div");
// This div is of class product
divElement.setAttribute("class", "product");
//...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.