Skip to content

Commit 6c111f7

Browse files
committed
Adding code for chapter 12
1 parent e46baf5 commit 6c111f7

File tree

7 files changed

+409
-0
lines changed

7 files changed

+409
-0
lines changed

chapter12/cadi.html

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Car Constructor</title>
6+
<script>
7+
8+
var cadiParams = {make: "GM",
9+
model: "Cadillac",
10+
year: 1955,
11+
color: "tan",
12+
passengers: 5,
13+
convertible: false,
14+
mileage: 12892};
15+
16+
var cadi = new Car(cadiParams);
17+
18+
function Car(params) {
19+
this.make = params.make;
20+
this.model = params.model;
21+
this.year = params.year;
22+
this.color = params.color;
23+
this.passengers = params.passengers;
24+
this.convertible = params.convertible;
25+
this.mileage = params.mileage;
26+
this.started = false;
27+
28+
this.start = function() {
29+
this.started = true;
30+
};
31+
this.stop = function() {
32+
this.started = false;
33+
};
34+
this.drive = function() {
35+
if (this.started) {
36+
alert("Zoom zoom!");
37+
} else {
38+
alert("You need to start the engine first.");
39+
}
40+
};
41+
}
42+
43+
cadi.start();
44+
cadi.drive();
45+
cadi.drive();
46+
cadi.stop();
47+
48+
</script>
49+
</head>
50+
<body>
51+
</body>
52+
</html>
53+

chapter12/car.html

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Car Constructor</title>
6+
<script>
7+
8+
function Car(make, model, year, color, passengers, convertible, mileage) {
9+
this.make = make;
10+
this.model = model;
11+
this.year = year;
12+
this.color = color;
13+
this.passengers = passengers;
14+
this.convertible = convertible;
15+
this.mileage = mileage;
16+
this.started = false;
17+
18+
this.start = function() {
19+
this.started = true;
20+
};
21+
22+
this.stop = function() {
23+
this.started = false;
24+
};
25+
26+
this.drive = function() {
27+
if (this.started) {
28+
console.log(this.make + " " + this.model + " goes zoom zoom!");
29+
} else {
30+
console.log("Start the engine first.");
31+
}
32+
};
33+
34+
}
35+
36+
var chevy = new Car("Chevy", "Bel Air", 1957, "red", 2, false, 1021);
37+
var cadi = new Car("GM", "Cadillac", 1955, "tan", 5, false, 12892);
38+
var taxi = new Car("Webville Motors", "Taxi", 1955, "yellow", 4, false, 281341);
39+
var fiat = new Car("Fiat", "500", 1957, "Medium Blue", 2, false, 88000);
40+
41+
var testCar = new Car("Webville Motors", "Test Car", 2014, "marine", 2, true, 21);
42+
43+
var cars = [chevy, cadi, taxi, fiat, testCar];
44+
45+
for(var i = 0; i < cars.length; i++) {
46+
cars[i].start();
47+
cars[i].drive();
48+
cars[i].drive();
49+
cars[i].stop();
50+
}
51+
52+
53+
</script>
54+
</head>
55+
<body>
56+
</body>
57+
</html>
58+

chapter12/carAndDog.html

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Car Constructor</title>
6+
<script>
7+
8+
function Car(params) {
9+
this.make = params.make;
10+
this.model = params.model;
11+
this.year = params.year;
12+
this.color = params.color;
13+
this.passengers = params.passengers;
14+
this.convertible = params.convertible;
15+
this.mileage = params.mileage;
16+
this.started = false;
17+
18+
this.start = function() {
19+
this.started = true;
20+
};
21+
22+
this.stop = function() {
23+
this.started = false;
24+
};
25+
26+
this.drive = function() {
27+
if (this.started) {
28+
console.log(this.make + " " + this.model + " goes zoom zoom!");
29+
} else {
30+
console.log("Start the engine first.");
31+
}
32+
};
33+
34+
}
35+
36+
var cadiParams = {make: "GM",
37+
model: "Cadillac",
38+
year: 1955,
39+
color: "tan",
40+
passengers: 5,
41+
convertible: false,
42+
miles: 12892};
43+
var cadi = new Car(cadiParams);
44+
45+
var chevyParams = {make: "Chevy",
46+
model: "Bel Air",
47+
year: 1957,
48+
color: "red",
49+
passengers: 2,
50+
convertible: false,
51+
miles: 1021};
52+
var chevy = new Car(chevyParams);
53+
54+
var taxiParams= {make: "Webville Motors",
55+
model: "Taxi",
56+
year: 1955,
57+
color: "yellow",
58+
passengers: 4,
59+
convertible: false,
60+
miles: 281341};
61+
var taxi = new Car(taxiParams);
62+
63+
var fiatParams= {make: "Webville Motors",
64+
model: "500",
65+
year: 1957,
66+
color: "Medium Blue",
67+
passengers: 2,
68+
convertible: false,
69+
miles: 88000};
70+
var fiat = new Car("Fiat", "500", 1957, "Medium Blue", 2, false, 88000);
71+
72+
var testParams= {make: "Webville Motors",
73+
model: "Test Car",
74+
year: 2014,
75+
color: "marine",
76+
passengers: 2,
77+
convertible: true,
78+
miles: 21};
79+
var testCar = new Car("WebVille Motors", "Test Car", 2014, "marine", 2, true, 21);
80+
81+
var cars = [chevy, cadi, taxi, fiat, testCar];
82+
83+
/*
84+
* Commented out so we don't have to see all the alerts again!
85+
*
86+
for(var i = 0; i < cars.length; i++) {
87+
cars[i].start();
88+
cars[i].drive();
89+
cars[i].drive();
90+
cars[i].stop();
91+
}
92+
*/
93+
94+
function Dog(name, breed, weight) {
95+
this.name = name;
96+
this.breed = breed;
97+
this.weight = weight;
98+
this.bark = function() {
99+
if (this.weight > 25) {
100+
alert(this.name + " says Woof!");
101+
} else {
102+
alert(this.name + " says Yip!");
103+
}
104+
};
105+
}
106+
107+
var limoParams = {make: "Webville Motors",
108+
model: "limo",
109+
year: 1983,
110+
color: "black",
111+
passengers: 12,
112+
convertible: true,
113+
mileage: 21120};
114+
115+
var limo = new Car(limoParams);
116+
var limoDog = new Dog("Rhapsody In Blue", "Poodle", 40);
117+
118+
console.log(limo.make + " " + limo.model + " is a " + typeof limo);
119+
console.log(limoDog.name + " is a " + typeof limoDog);
120+
121+
</script>
122+
</head>
123+
<body>
124+
</body>
125+
</html>
126+
127+
</script>
128+
</head>
129+
<body>
130+
</body>
131+
</html>
132+

chapter12/coffee.html

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Coffee Constructor</title>
6+
<script>
7+
8+
function Coffee(roast, ounces) {
9+
this.roast = roast;
10+
this.ounces = ounces;
11+
this.getSize = function() {
12+
if (this.ounces === 8) {
13+
return "small";
14+
} else if (this.ounces === 12) {
15+
return "medium";
16+
} else if (this.ounces === 16) {
17+
return "large";
18+
}
19+
};
20+
this.toString = function() {
21+
return "You've ordered a " + this.getSize() + " " + this.roast + " coffee.";
22+
};
23+
}
24+
25+
var coffee = new Coffee("House Blend", 12);
26+
console.log(coffee.toString());
27+
28+
var darkRoast = new Coffee("Dark Roast", 16);
29+
console.log(darkRoast.toString());
30+
31+
</script>
32+
</head>
33+
<body>
34+
</body>
35+
</html>
36+

chapter12/dog1.html

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Construct some dogs</title>
6+
<script>
7+
8+
function Dog(name, breed, weight) {
9+
this.name = name;
10+
this.breed = breed;
11+
this.weight = weight;
12+
}
13+
var fido = new Dog("Fido", "Mixed", 38);
14+
var fluffy = new Dog("Fluffy", "Poodle", 30);
15+
var spot = new Dog("Spot", "Chihuahua", 10);
16+
var dogs = [fido, fluffy, spot];
17+
18+
for(var i = 0; i < dogs.length; i++) {
19+
var size = "small";
20+
if (dogs[i].weight > 10) {
21+
size = "large";
22+
}
23+
console.log("Dog: " + dogs[i].name
24+
+ " is a " + size
25+
+ " " + dogs[i].breed);
26+
}
27+
28+
</script>
29+
</head>
30+
<body>
31+
</body>
32+
</html>
33+

chapter12/dog2.html

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Construct some dogs</title>
6+
<script>
7+
8+
function Dog(name, breed, weight) {
9+
this.name = name;
10+
this.breed = breed;
11+
this.weight = weight;
12+
this.bark = function() {
13+
if (this.weight > 25) {
14+
alert(this.name + " says Woof!");
15+
} else {
16+
alert(this.name + " says Yip!");
17+
}
18+
};
19+
}
20+
var fido = new Dog("Fido", "Mixed", 38);
21+
var fluffy = new Dog("Fluffy", "Poodle", 30);
22+
var spot = new Dog("Spot", "Chihuahua", 10);
23+
var dogs = [fido, fluffy, spot];
24+
25+
for(var i = 0; i < dogs.length; i++) {
26+
var size = "small";
27+
if (dogs[i].weight > 10) {
28+
size = "large";
29+
}
30+
console.log("Dog: " + dogs[i].name
31+
+ " is a " + size
32+
+ " " + dogs[i].breed);
33+
}
34+
35+
for (var i = 0; i < dogs.length; i++) {
36+
dogs[i].bark();
37+
}
38+
39+
</script>
40+
</head>
41+
<body>
42+
</body>
43+
</html>
44+

0 commit comments

Comments
 (0)