Skip to content

Commit 8552874

Browse files
committed
Adding chapter 10 code
1 parent 2749640 commit 8552874

File tree

6 files changed

+350
-0
lines changed

6 files changed

+350
-0
lines changed

chapter10/addN.html

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Return a function from a function</title>
6+
<script>
7+
8+
function addN(n) {
9+
var adder = function(x) {
10+
return n + x;
11+
};
12+
return adder;
13+
}
14+
15+
var add2 = addN(2);
16+
console.log(add2(10));
17+
console.log(add2(100));
18+
19+
var add100 = addN(100);
20+
console.log(add100(1));
21+
console.log(add100(50));
22+
23+
</script>
24+
</head>
25+
<body> </body>
26+
</html>
27+

chapter10/aloha.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Aloha</title>
6+
<script>
7+
8+
function sayIt(translator) {
9+
var phrase = translator("Hello");
10+
alert(phrase);
11+
}
12+
13+
function hawaiianTranslator(word) {
14+
if (word == "Hello") return "Aloha";
15+
if (word == "Goodbye") return "Aloha";
16+
}
17+
18+
sayIt(hawaiianTranslator);
19+
20+
</script>
21+
</head>
22+
<body> </body>
23+
</html>
24+

chapter10/cola.html

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>Webville Cola</title>
5+
<meta charset="utf-8">
6+
<script>
7+
8+
var products = [ { name: "Grapefruit", calories: 170, color: "red", sold: 8200 },
9+
{ name: "Orange", calories: 160, color: "orange", sold: 12101 },
10+
{ name: "Cola", calories: 210, color: "caramel", sold: 25412 },
11+
{ name: "Diet Cola", calories: 0, color: "caramel", sold: 43922 },
12+
{ name: "Lemon", calories: 200, color: "clear", sold: 14983 },
13+
{ name: "Raspberry", calories: 180, color: "pink", sold: 9427 },
14+
{ name: "Root Beer", calories: 200, color: "caramel", sold: 9909 },
15+
{ name: "Water", calories: 0, color: "clear", sold: 62123 }
16+
];
17+
18+
// sort and display the scores
19+
console.log("\n------- sorting by sold -------");
20+
products.sort(compareSold);
21+
printProducts(products);
22+
23+
console.log("\n------- sorting by name -------");
24+
products.sort(compareName);
25+
printProducts(products);
26+
27+
console.log("\n------- sorting by calories -------");
28+
products.sort(compareCalories);
29+
printProducts(products);
30+
31+
console.log("\n------- sorting by color -------");
32+
products.sort(compareColor);
33+
printProducts(products);
34+
35+
function compareName(colaA, colaB) {
36+
if (colaA.name > colaB.name) {
37+
return 1;
38+
} else if (colaA.name === colaB.name) {
39+
return 0;
40+
} else {
41+
return -1;
42+
}
43+
}
44+
45+
function compareCalories(colaA, colaB) {
46+
if (colaA.calories > colaB.calories) {
47+
return 1;
48+
} else if (colaA.calories === colaB.calories) {
49+
return 0;
50+
} else {
51+
return -1;
52+
}
53+
}
54+
55+
function compareColor(colaA, colaB) {
56+
if (colaA.color > colaB.color) {
57+
return 1;
58+
} else if (colaA.color === colaB.color) {
59+
return 0;
60+
} else {
61+
return -1;
62+
}
63+
}
64+
65+
function compareSold(colaA, colaB) {
66+
if (colaA.sold > colaB.sold) {
67+
return 1;
68+
} else if (colaA.sold === colaB.sold) {
69+
return 0;
70+
} else {
71+
return -1;
72+
}
73+
}
74+
75+
function printProducts(products) {
76+
for (var i = 0; i < products.length; i++) {
77+
console.log("Name: " + products[i].name +
78+
", Calories: " + products[i].calories +
79+
", Color: " + products[i].color +
80+
", Sold: " + products[i].sold);
81+
}
82+
}
83+
84+
85+
</script>
86+
</head>
87+
<body> </body>
88+
</html>
89+

chapter10/fun.html

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Fun</title>
6+
<script>
7+
8+
function fun(echo) {
9+
console.log(echo);
10+
};
11+
12+
fun("hello");
13+
14+
function boo(aFunction) {
15+
aFunction("boo");
16+
}
17+
18+
boo(fun);
19+
20+
console.log(fun);
21+
22+
fun(boo);
23+
24+
var moreFun = fun;
25+
26+
moreFun("hello again");
27+
28+
function echoMaker() {
29+
return fun;
30+
}
31+
32+
var bigFun = echoMaker();
33+
bigFun("Is there an echo?");
34+
35+
36+
</script>
37+
</head>
38+
<body>
39+
</body>
40+
</html>
41+

chapter10/plane.html

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>First class functions</title>
6+
<script>
7+
8+
var passengers = [ { name: "Jane Doloop", paid: true, ticket: "coach" },
9+
{ name: "Dr. Evel", paid: true, ticket: "firstclass" },
10+
{ name: "Sue Property", paid: false, ticket: "firstclass" },
11+
{ name: "John Funcall", paid: true, ticket: "premium" } ];
12+
13+
function processPassengers(passengers, test) {
14+
for (var i = 0; i < passengers.length; i++) {
15+
if (test(passengers[i])) {
16+
return false;
17+
}
18+
}
19+
return true;
20+
}
21+
22+
function checkNoFlyList(passenger) {
23+
return (passenger.name === "Dr. Evel");
24+
}
25+
26+
function checkNotPaid(passenger) {
27+
return (!passenger.paid);
28+
}
29+
30+
function printPassenger(passenger) {
31+
var message = passenger.name;
32+
if (passenger.paid === true) {
33+
message = message + " has paid";
34+
} else {
35+
message = message + " has not paid";
36+
}
37+
console.log(message);
38+
return false;
39+
}
40+
41+
//
42+
// plane can only fly if every passenger is on the fly flist
43+
//
44+
var allCanFly = processPassengers(passengers, checkNoFlyList);
45+
if (!allCanFly) {
46+
console.log("The plane can't take off: we have a passenger on the no fly list.");
47+
}
48+
49+
//
50+
// plane can only fly if every passenger has paid
51+
//
52+
var allPaid = processPassengers(passengers, checkNotPaid);
53+
if (!allPaid) {
54+
console.log("The plane can't take off: not everyone has paid.");
55+
}
56+
57+
//
58+
// we don't care about the result here; we're just using
59+
// processPassengers to display the passenger list
60+
//
61+
processPassengers(passengers, printPassenger);
62+
63+
function createDrinkOrder(passenger) {
64+
var orderFunction;
65+
if (passenger.ticket === "firstclass") {
66+
orderFunction = function() {
67+
alert("Would you like a cocktail or wine?");
68+
};
69+
} else if (passenger.ticket === "premium") {
70+
orderFunction = function() {
71+
alert("Would you like wine, cola or water?");
72+
};
73+
} else {
74+
orderFunction = function() {
75+
alert("Your choice is cola or water.");
76+
};
77+
}
78+
return orderFunction;
79+
}
80+
81+
82+
function createDinnerOrder(passenger) {
83+
var orderFunction;
84+
if (passenger.ticket === "firstclass") {
85+
orderFunction = function() {
86+
alert("Would you like chicken or pasta?");
87+
};
88+
} else if (passenger.ticket === "premium") {
89+
orderFunction = function() {
90+
alert("Would you like a snack box or cheese plate?");
91+
};
92+
} else {
93+
orderFunction = function() {
94+
alert("Would you like peanuts or pretzels?");
95+
};
96+
}
97+
return orderFunction;
98+
}
99+
100+
function pickupTrash() {
101+
alert("Can I have your trash, please?");
102+
}
103+
104+
function serveCustomer(passenger) {
105+
var getDrinkOrderFunction = createDrinkOrder(passenger);
106+
var getDinnerOrderFunction = createDinnerOrder(passenger);
107+
108+
getDrinkOrderFunction();
109+
110+
// get dinner order
111+
getDinnerOrderFunction();
112+
113+
getDrinkOrderFunction();
114+
getDrinkOrderFunction();
115+
116+
// show movie
117+
118+
getDrinkOrderFunction();
119+
120+
// pick up trash
121+
pickupTrash();
122+
}
123+
124+
function servePassengers(passengers) {
125+
for (var i = 0; i < passengers.length; i++) {
126+
serveCustomer(passengers[i]);
127+
}
128+
}
129+
130+
servePassengers(passengers);
131+
132+
</script>
133+
</head>
134+
<body>
135+
</body>
136+
</html>
137+

chapter10/shellgame.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Shell Game</title>
6+
<script>
7+
8+
var winner = function() { alert("WINNER!") };
9+
var loser = function() { alert("LOSER!") };
10+
// let's test as a warm up
11+
winner();
12+
// let's assign to other variables for practice
13+
var a = winner;
14+
var b = loser;
15+
var c = loser;
16+
a();
17+
b();
18+
// now let's try your luck with a shall game
19+
c = a;
20+
a = b;
21+
b = c;
22+
c = a;
23+
a = c;
24+
a = b;
25+
b = c;
26+
a();
27+
28+
</script>
29+
</head>
30+
<body> </body>
31+
</html>
32+

0 commit comments

Comments
 (0)