Skip to content

Commit

Permalink
Adding code for chapter 12
Browse files Browse the repository at this point in the history
  • Loading branch information
bethrobson committed Feb 6, 2014
1 parent e46baf5 commit 6c111f7
Show file tree
Hide file tree
Showing 7 changed files with 409 additions and 0 deletions.
53 changes: 53 additions & 0 deletions chapter12/cadi.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Car Constructor</title>
<script>

var cadiParams = {make: "GM",
model: "Cadillac",
year: 1955,
color: "tan",
passengers: 5,
convertible: false,
mileage: 12892};

var cadi = new Car(cadiParams);

function Car(params) {
this.make = params.make;
this.model = params.model;
this.year = params.year;
this.color = params.color;
this.passengers = params.passengers;
this.convertible = params.convertible;
this.mileage = params.mileage;
this.started = false;

this.start = function() {
this.started = true;
};
this.stop = function() {
this.started = false;
};
this.drive = function() {
if (this.started) {
alert("Zoom zoom!");
} else {
alert("You need to start the engine first.");
}
};
}

cadi.start();
cadi.drive();
cadi.drive();
cadi.stop();

</script>
</head>
<body>
</body>
</html>

58 changes: 58 additions & 0 deletions chapter12/car.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Car Constructor</title>
<script>

function Car(make, model, year, color, passengers, convertible, mileage) {
this.make = make;
this.model = model;
this.year = year;
this.color = color;
this.passengers = passengers;
this.convertible = convertible;
this.mileage = mileage;
this.started = false;

this.start = function() {
this.started = true;
};

this.stop = function() {
this.started = false;
};

this.drive = function() {
if (this.started) {
console.log(this.make + " " + this.model + " goes zoom zoom!");
} else {
console.log("Start the engine first.");
}
};

}

var chevy = new Car("Chevy", "Bel Air", 1957, "red", 2, false, 1021);
var cadi = new Car("GM", "Cadillac", 1955, "tan", 5, false, 12892);
var taxi = new Car("Webville Motors", "Taxi", 1955, "yellow", 4, false, 281341);
var fiat = new Car("Fiat", "500", 1957, "Medium Blue", 2, false, 88000);

var testCar = new Car("Webville Motors", "Test Car", 2014, "marine", 2, true, 21);

var cars = [chevy, cadi, taxi, fiat, testCar];

for(var i = 0; i < cars.length; i++) {
cars[i].start();
cars[i].drive();
cars[i].drive();
cars[i].stop();
}


</script>
</head>
<body>
</body>
</html>

132 changes: 132 additions & 0 deletions chapter12/carAndDog.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Car Constructor</title>
<script>

function Car(params) {
this.make = params.make;
this.model = params.model;
this.year = params.year;
this.color = params.color;
this.passengers = params.passengers;
this.convertible = params.convertible;
this.mileage = params.mileage;
this.started = false;

this.start = function() {
this.started = true;
};

this.stop = function() {
this.started = false;
};

this.drive = function() {
if (this.started) {
console.log(this.make + " " + this.model + " goes zoom zoom!");
} else {
console.log("Start the engine first.");
}
};

}

var cadiParams = {make: "GM",
model: "Cadillac",
year: 1955,
color: "tan",
passengers: 5,
convertible: false,
miles: 12892};
var cadi = new Car(cadiParams);

var chevyParams = {make: "Chevy",
model: "Bel Air",
year: 1957,
color: "red",
passengers: 2,
convertible: false,
miles: 1021};
var chevy = new Car(chevyParams);

var taxiParams= {make: "Webville Motors",
model: "Taxi",
year: 1955,
color: "yellow",
passengers: 4,
convertible: false,
miles: 281341};
var taxi = new Car(taxiParams);

var fiatParams= {make: "Webville Motors",
model: "500",
year: 1957,
color: "Medium Blue",
passengers: 2,
convertible: false,
miles: 88000};
var fiat = new Car("Fiat", "500", 1957, "Medium Blue", 2, false, 88000);

var testParams= {make: "Webville Motors",
model: "Test Car",
year: 2014,
color: "marine",
passengers: 2,
convertible: true,
miles: 21};
var testCar = new Car("WebVille Motors", "Test Car", 2014, "marine", 2, true, 21);

var cars = [chevy, cadi, taxi, fiat, testCar];

/*
* Commented out so we don't have to see all the alerts again!
*
for(var i = 0; i < cars.length; i++) {
cars[i].start();
cars[i].drive();
cars[i].drive();
cars[i].stop();
}
*/

function Dog(name, breed, weight) {
this.name = name;
this.breed = breed;
this.weight = weight;
this.bark = function() {
if (this.weight > 25) {
alert(this.name + " says Woof!");
} else {
alert(this.name + " says Yip!");
}
};
}

var limoParams = {make: "Webville Motors",
model: "limo",
year: 1983,
color: "black",
passengers: 12,
convertible: true,
mileage: 21120};

var limo = new Car(limoParams);
var limoDog = new Dog("Rhapsody In Blue", "Poodle", 40);

console.log(limo.make + " " + limo.model + " is a " + typeof limo);
console.log(limoDog.name + " is a " + typeof limoDog);

</script>
</head>
<body>
</body>
</html>

</script>
</head>
<body>
</body>
</html>

36 changes: 36 additions & 0 deletions chapter12/coffee.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Coffee Constructor</title>
<script>

function Coffee(roast, ounces) {
this.roast = roast;
this.ounces = ounces;
this.getSize = function() {
if (this.ounces === 8) {
return "small";
} else if (this.ounces === 12) {
return "medium";
} else if (this.ounces === 16) {
return "large";
}
};
this.toString = function() {
return "You've ordered a " + this.getSize() + " " + this.roast + " coffee.";
};
}

var coffee = new Coffee("House Blend", 12);
console.log(coffee.toString());

var darkRoast = new Coffee("Dark Roast", 16);
console.log(darkRoast.toString());

</script>
</head>
<body>
</body>
</html>

33 changes: 33 additions & 0 deletions chapter12/dog1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Construct some dogs</title>
<script>

function Dog(name, breed, weight) {
this.name = name;
this.breed = breed;
this.weight = weight;
}
var fido = new Dog("Fido", "Mixed", 38);
var fluffy = new Dog("Fluffy", "Poodle", 30);
var spot = new Dog("Spot", "Chihuahua", 10);
var dogs = [fido, fluffy, spot];

for(var i = 0; i < dogs.length; i++) {
var size = "small";
if (dogs[i].weight > 10) {
size = "large";
}
console.log("Dog: " + dogs[i].name
+ " is a " + size
+ " " + dogs[i].breed);
}

</script>
</head>
<body>
</body>
</html>

44 changes: 44 additions & 0 deletions chapter12/dog2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Construct some dogs</title>
<script>

function Dog(name, breed, weight) {
this.name = name;
this.breed = breed;
this.weight = weight;
this.bark = function() {
if (this.weight > 25) {
alert(this.name + " says Woof!");
} else {
alert(this.name + " says Yip!");
}
};
}
var fido = new Dog("Fido", "Mixed", 38);
var fluffy = new Dog("Fluffy", "Poodle", 30);
var spot = new Dog("Spot", "Chihuahua", 10);
var dogs = [fido, fluffy, spot];

for(var i = 0; i < dogs.length; i++) {
var size = "small";
if (dogs[i].weight > 10) {
size = "large";
}
console.log("Dog: " + dogs[i].name
+ " is a " + size
+ " " + dogs[i].breed);
}

for (var i = 0; i < dogs.length; i++) {
dogs[i].bark();
}

</script>
</head>
<body>
</body>
</html>

Loading

0 comments on commit 6c111f7

Please sign in to comment.