Skip to content

Commit 8d3bff5

Browse files
committed
adding chapter 3 code
1 parent 439edf8 commit 8d3bff5

File tree

5 files changed

+167
-0
lines changed

5 files changed

+167
-0
lines changed

chapter3/areaAndDistance.html

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Area and Distance</title>
6+
</head>
7+
<body>
8+
</body>
9+
<script>
10+
var x = 32;
11+
var y = 44;
12+
var radius = 5;
13+
14+
var centerX = 0;
15+
var centerY = 0;
16+
var width = 600;
17+
var height = 400;
18+
19+
function setup(width, height) {
20+
centerX = width/2;
21+
centerY = height/2;
22+
}
23+
24+
function computeDistance(x1, y1, x2, y2) {
25+
var dx = x1 - x2;
26+
var dy = y1 - y2;
27+
var d2 = (dx * dx) + (dy * dy);
28+
var d = Math.sqrt(d2);
29+
return d;
30+
}
31+
32+
function circleArea(r) {
33+
var area = Math.PI * r * r;
34+
return area;
35+
}
36+
37+
setup(width, height);
38+
var area = circleArea(radius);
39+
var distance = computeDistance(x, y, centerX, centerY);
40+
alert("Area: " + area);
41+
alert("Distance: " + distance);
42+
</script>
43+
</body>
44+
</html>

chapter3/bake.html

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Bake (with return)</title>
6+
</head>
7+
<body>
8+
</body>
9+
<script>
10+
function bake(degrees) {
11+
var message;
12+
13+
if (degrees > 500) {
14+
message = "I'm not a nuclear reactor!";
15+
} else if (degrees < 100) {
16+
message = "I'm not a refrigerator!";
17+
} else {
18+
message = "That's a very comfortable temperature for me.";
19+
setMode("bake");
20+
setTemp(degrees);
21+
}
22+
return message;
23+
}
24+
25+
var status = bake(350);
26+
27+
// Here so the code will run!
28+
function setMode(mode) { };
29+
function setTemp(degrees) { };
30+
31+
</script>
32+
</body>
33+
</html>

chapter3/bark.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Bark</title>
6+
</head>
7+
<body>
8+
</body>
9+
<script>
10+
function bark(name, weight) {
11+
if (weight > 20) {
12+
console.log(name + " says WOOF WOOF");
13+
} else {
14+
console.log(name + " says woof woof");
15+
}
16+
}
17+
bark("rover", 23);
18+
19+
bark("spot", 13);
20+
21+
bark("spike", 53);
22+
23+
bark("lady", 17);
24+
</script>
25+
</body>
26+
</html>

chapter3/sherluck.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Five Minute Mystery</title>
6+
</head>
7+
<body>
8+
</body>
9+
<script>
10+
var balance = 10500;
11+
var cameraOn = true;
12+
13+
function steal(balance, amount) {
14+
cameraOn = false;
15+
if (amount < balance) {
16+
balance = balance - amount;
17+
}
18+
return amount;
19+
cameraOn = true;
20+
}
21+
22+
var amount = steal(balance, 1250);
23+
alert("Criminal: you stole " + amount + "!");
24+
</script>
25+
</body>
26+
</html>

chapter3/thingamajig.html

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>The Thing-a-ma-jig</title>
6+
</head>
7+
<body>
8+
</body>
9+
<script>
10+
function clunk(times) {
11+
var num = times;
12+
while (num > 0) {
13+
console.log("clunk");
14+
clunkCounter = clunkCounter + 1;
15+
num = num - 1;
16+
}
17+
}
18+
19+
function thingamajig(size) {
20+
var facky = 1;
21+
if (size == 0) {
22+
console.log("clank");
23+
} else if (size == 1) {
24+
console.log("thunk");
25+
} else {
26+
while (size > 1) {
27+
facky = facky * size;
28+
size = size - 1;
29+
}
30+
clunk(facky);
31+
}
32+
}
33+
var clunkCounter = 0;
34+
thingamajig(5);
35+
console.log(clunkCounter);
36+
</script>
37+
</body>
38+
</html>

0 commit comments

Comments
 (0)