Skip to content

Commit 5bffc95

Browse files
committed
adding code for chapter 5
1 parent 49be63d commit 5bffc95

File tree

6 files changed

+406
-0
lines changed

6 files changed

+406
-0
lines changed

chapter5/autoomatic.html

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<title>Object-o-matic</title>
5+
<meta charset="utf-8">
6+
<script>
7+
function makeCar() {
8+
var makes = ["Chevy", "Ford", "Fiat", "Webville Motors", "Tucker"];
9+
var models = ["Cadillac", "500", "Bel-Air", "Taxi", "Torpedo"];
10+
var years = ["1955", "1957", "1948", "1954", "1961"];
11+
12+
var rand1 = Math.floor(Math.random() * makes.length);
13+
var rand2 = Math.floor(Math.random() * models.length);
14+
var rand3 = Math.floor(Math.random() * years.length);
15+
16+
var imaginaryCar = {
17+
make: makes[rand1],
18+
model: models[rand2],
19+
year: years[rand3]
20+
};
21+
22+
return imaginaryCar;
23+
}
24+
25+
function displayCar(car) {
26+
console.log("Your imaginary car is a " + car.year + " " + car.make + " " + car.model);
27+
}
28+
29+
var myImaginaryCar = makeCar();
30+
displayCar(myImaginaryCar);
31+
32+
</script>
33+
</head>
34+
<body></body>
35+
</html>

chapter5/carWithDrive.html

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>Car with drive</title>
5+
<meta charset="utf-8">
6+
<script>
7+
8+
var fiat = {
9+
make: "Fiat",
10+
model: "500",
11+
year: 1957,
12+
color: "Medium Blue",
13+
passengers: 2,
14+
convertible: false,
15+
mileage: 88000,
16+
started: false,
17+
18+
start: function() {
19+
this.started = true;
20+
},
21+
22+
stop: function() {
23+
this.started = false;
24+
},
25+
26+
drive: function() {
27+
if (this.started) {
28+
alert("Zoom zoom!");
29+
} else {
30+
alert("You need to start the engine first.");
31+
}
32+
}
33+
};
34+
35+
fiat.drive();
36+
fiat.start();
37+
fiat.drive();
38+
fiat.stop();
39+
40+
</script>
41+
</head>
42+
<body>
43+
</body>
44+
</html>
45+

chapter5/carWithDriveExercise.html

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>All cars driving</title>
5+
<meta charset="utf-8">
6+
<script>
7+
8+
var fiat = {
9+
make: "Fiat",
10+
model: "500",
11+
year: 1957,
12+
color: "Medium Blue",
13+
passengers: 2,
14+
convertible: false,
15+
mileage: 88000,
16+
started: false,
17+
start: function() {
18+
this.started = true;
19+
},
20+
stop: function() {
21+
this.started = false;
22+
},
23+
drive: function() {
24+
if (this.started) {
25+
alert(this.make + " " +
26+
this.model + " goes zoom zoom!");
27+
} else {
28+
alert("You need to start the engine first.");
29+
}
30+
}
31+
};
32+
33+
var cadi = {
34+
make: "Ford",
35+
model: "Cadillac",
36+
year: 1955,
37+
color: "tan",
38+
passengers: 5,
39+
convertible: false,
40+
mileage: 12892,
41+
started: false,
42+
start: function() {
43+
this.started = true;
44+
},
45+
stop: function() {
46+
this.started = false;
47+
},
48+
drive: function() {
49+
if (this.started) {
50+
alert(this.make + " " +
51+
this.model + " goes zoom zoom!");
52+
} else {
53+
alert("You need to start the engine first.");
54+
}
55+
}
56+
};
57+
58+
59+
var chevy = {
60+
make: "Chevy",
61+
model: "Bel Air",
62+
year: 1957,
63+
color: "red",
64+
passengers: 2,
65+
convertible: false,
66+
mileage: 1021,
67+
started: false,
68+
start: function() {
69+
this.started = true;
70+
},
71+
stop: function() {
72+
this.started = false;
73+
},
74+
drive: function() {
75+
if (this.started) {
76+
alert(this.make + " " +
77+
this.model + " goes zoom zoom!");
78+
} else {
79+
alert("You need to start the engine first.");
80+
}
81+
}
82+
};
83+
84+
85+
86+
var taxi = {
87+
make: "WebVille Motors",
88+
model: "Taxi",
89+
year: 1955,
90+
color: "yellow",
91+
passengers: 4,
92+
convertible: false,
93+
mileage: 281341,
94+
started: false,
95+
start: function() {
96+
this.started = true;
97+
},
98+
stop: function() {
99+
this.started = false;
100+
},
101+
drive: function() {
102+
if (this.started) {
103+
alert(this.make + " " +
104+
this.model + " goes zoom zoom!");
105+
} else {
106+
alert("You need to start the engine first.");
107+
}
108+
}
109+
};
110+
111+
fiat.start();
112+
fiat.drive();
113+
fiat.stop();
114+
115+
cadi.start();
116+
cadi.drive();
117+
cadi.stop();
118+
119+
chevy.start();
120+
chevy.drive();
121+
chevy.stop();
122+
123+
taxi.start();
124+
taxi.drive();
125+
taxi.stop();
126+
127+
</script>
128+
</head>
129+
<body>
130+
</body>
131+
</html>
132+

chapter5/carWithFuel.html

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>Car with fuel</title>
5+
<meta charset="utf-8">
6+
<script>
7+
8+
var fiat = {
9+
make: "Fiat",
10+
model: "500",
11+
year: 1957,
12+
color: "Medium Blue",
13+
passengers: 2,
14+
convertible: false,
15+
mileage: 88000,
16+
fuel: 0,
17+
started: false,
18+
19+
start: function() {
20+
if (this.fuel == 0) {
21+
alert("The car is on empty, fill up before starting!");
22+
} else {
23+
this.started = true;
24+
}
25+
},
26+
27+
stop: function() {
28+
this.started = false;
29+
},
30+
31+
drive: function() {
32+
if (this.started) {
33+
if (this.fuel > 0) {
34+
alert(this.make + " " +
35+
this.model + " goes zoom zoom!");
36+
this.fuel = this.fuel - 1;
37+
} else {
38+
alert("Uh oh, out of fuel.");
39+
this.stop();
40+
}
41+
} else {
42+
alert("You need to start the engine first.");
43+
}
44+
},
45+
46+
addFuel: function(amount) {
47+
this.fuel = this.fuel + amount;
48+
}
49+
};
50+
51+
fiat.start();
52+
fiat.drive();
53+
fiat.addFuel(2);
54+
fiat.start();
55+
fiat.drive();
56+
fiat.drive();
57+
fiat.drive();
58+
fiat.stop();
59+
60+
</script>
61+
</head>
62+
<body>
63+
</body>
64+
</html>
65+

chapter5/prequal.html

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>Prequalifcation</title>
5+
<meta charset="utf-8">
6+
<script>
7+
8+
var chevy = {
9+
make: "Chevy",
10+
model: "Bel Air",
11+
year: 1957,
12+
color: "red",
13+
passengers: 2,
14+
convertible: false,
15+
mileage: 1021
16+
};
17+
18+
var fiat = {
19+
make: "Fiat",
20+
model: "500",
21+
year: 1957,
22+
color: "Medium Blue",
23+
passengers: 2,
24+
convertible: false,
25+
mileage: 88000
26+
};
27+
28+
var cadi = {
29+
make: "Ford",
30+
model: "Cadillac",
31+
year: 1955,
32+
color: "tan",
33+
passengers: 5,
34+
convertible: false,
35+
mileage: 12892
36+
};
37+
38+
39+
var taxi = {
40+
make: "WebVille Motors",
41+
model: "Taxi",
42+
year: 1955,
43+
color: "yellow",
44+
passengers: 4,
45+
convertible: false,
46+
mileage: 281341
47+
};
48+
49+
function prequal(car) {
50+
if (car.mileage > 10000) {
51+
return false;
52+
} else if (car.year > 1960) {
53+
return false;
54+
}
55+
return true;
56+
}
57+
58+
var worthALook = prequal(taxi);
59+
60+
if (worthALook) {
61+
console.log("You gotta check out this " + taxi.make + " " + taxi.model);
62+
} else {
63+
console.log("You should really pass on the " + taxi.make + " " + taxi.model);
64+
}
65+
66+
//
67+
// add a function to make it easier to generate the console output!
68+
//
69+
function isWorthALook(b, car) {
70+
if (b) {
71+
console.log("You gotta check out this " + car.make + " " + car.model);
72+
} else {
73+
console.log("You should really pass on the " + car.make + " " + car.model);
74+
}
75+
}
76+
isWorthALook(prequal(taxi), taxi);
77+
isWorthALook(prequal(cadi), cadi);
78+
isWorthALook(prequal(chevy), chevy);
79+
isWorthALook(prequal(fiat), fiat);
80+
81+
</script>
82+
</head>
83+
<body>
84+
</body>
85+
</html>
86+

0 commit comments

Comments
 (0)