Skip to content

Commit d3df683

Browse files
add error_handling
1 parent bbad1b8 commit d3df683

File tree

4 files changed

+262
-25
lines changed

4 files changed

+262
-25
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function MultiplicatorUnitFailure() {}
2+
3+
function primitiveMultiply(a, b) {
4+
if (Math.random() < 0.5)
5+
return a * b;
6+
else
7+
throw new MultiplicatorUnitFailure();
8+
}
9+
10+
function reliableMultiply(a, b) {
11+
var result;
12+
while(true){
13+
try{
14+
result = primitiveMultiply(a, b);
15+
break;
16+
}catch(error){
17+
if(error instanceof MultiplicatorUnitFailure){
18+
console.log("Error: MultiplicatorUnitFailure");
19+
}else{
20+
throw error;
21+
}
22+
}
23+
}
24+
return result;
25+
}
26+
27+
console.log(reliableMultiply(8, 8));
28+
// => 64
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var box = {
2+
locked: true,
3+
unlock: function() { this.locked = false; },
4+
lock: function() { this.locked = true; },
5+
_content: [],
6+
get content() {
7+
if (this.locked) throw new Error("Locked!");
8+
return this._content;
9+
}
10+
};
11+
12+
function withBoxUnlocked(body) {
13+
if(box.locked) box.unlock();
14+
try{
15+
body();
16+
}finally{
17+
box.lock();
18+
}
19+
}
20+
21+
withBoxUnlocked(function() {
22+
box.content.push("gold piece");
23+
});
24+
25+
try {
26+
withBoxUnlocked(function() {
27+
throw new Error("Pirates on the horizon! Abort!");
28+
});
29+
} catch (e) {
30+
console.log("Error raised:", e);
31+
}
32+
console.log(box.locked);
33+
// => true
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
var plan = ["############################",
2+
"# # # o ##",
3+
"# #",
4+
"# ##### #",
5+
"## # # ## #",
6+
"### ## # #",
7+
"# ### # #",
8+
"# #### #",
9+
"# ## o #",
10+
"# o # o ### #",
11+
"# # #",
12+
"############################"];
13+
14+
function Vector(x, y) {
15+
this.x = x;
16+
this.y = y;
17+
}
18+
19+
Vector.prototype.plus = function(other) {
20+
return new Vector(this.x + other.x, this.y + other.y);
21+
};
22+
23+
function Grid(width, height) {
24+
this.space = new Array(width * height);
25+
this.width = width;
26+
this.height = height;
27+
}
28+
Grid.prototype.isInside = function(vector) {
29+
return vector.x >= 0 && vector.x < this.width &&
30+
vector.y >= 0 && vector.y < this.height;
31+
};
32+
33+
Grid.prototype.get = function(vector) {
34+
return this.space[vector.x + this.width * vector.y];
35+
};
36+
37+
Grid.prototype.set = function(vector, value) {
38+
this.space[vector.x + this.width * vector.y] = value;
39+
};
40+
41+
var directions = {
42+
"n": new Vector( 0, -1),
43+
"ne": new Vector( 1, -1),
44+
"e": new Vector( 1, 0),
45+
"se": new Vector( 1, 1),
46+
"s": new Vector( 0, 1),
47+
"sw": new Vector(-1, 1),
48+
"w": new Vector(-1, 0),
49+
"nw": new Vector(-1, -1)
50+
};
51+
52+
function randomElement(array) {
53+
return array[Math.floor(Math.random() * array.length)];
54+
}
55+
56+
var directionNames = "n ne e se s sw w nw".split(" ");
57+
58+
function BouncingCritter() {
59+
this.direction = randomElement(directionNames);
60+
};
61+
62+
BouncingCritter.prototype.act = function(view) {
63+
if (view.look(this.direction) != " ")
64+
this.direction = view.find(" ") || "s";
65+
return {type: "move", direction: this.direction};
66+
};
67+
68+
function elementFromChar(legend, ch) {
69+
if (ch == " ")
70+
return null;
71+
var element = new legend[ch]();
72+
element.originChar = ch;
73+
return element;
74+
}
75+
76+
function World(map, legend) {
77+
var grid = new Grid(map[0].length, map.length);
78+
this.grid = grid;
79+
this.legend = legend;
80+
81+
map.forEach(function(line, y) {
82+
for (var x = 0; x < line.length; x++)
83+
grid.set(new Vector(x, y),
84+
elementFromChar(legend, line[x]));
85+
});
86+
}
87+
88+
function charFromElement(element) {
89+
if (element == null)
90+
return " ";
91+
else
92+
return element.originChar;
93+
}
94+
95+
World.prototype.toString = function() {
96+
var output = "";
97+
for (var y = 0; y < this.grid.height; y++) {
98+
for (var x = 0; x < this.grid.width; x++) {
99+
var element = this.grid.get(new Vector(x, y));
100+
output += charFromElement(element);
101+
}
102+
output += "\n";
103+
}
104+
return output;
105+
};
106+
107+
function Wall() {}
108+
109+
var world = new World(plan, {"#": Wall,
110+
"o": BouncingCritter});
111+
112+
Grid.prototype.forEach = function(f, context) {
113+
for (var y = 0; y < this.height; y++) {
114+
for (var x = 0; x < this.width; x++) {
115+
var value = this.space[x + y * this.width];
116+
if (value != null)
117+
f.call(context, value, new Vector(x, y));
118+
}
119+
}
120+
};
121+
122+
World.prototype.turn = function() {
123+
var acted = [];
124+
this.grid.forEach(function(critter, vector) {
125+
if (critter.act && acted.indexOf(critter) == -1) {
126+
acted.push(critter);
127+
this.letAct(critter, vector);
128+
}
129+
}, this);
130+
};
131+
132+
World.prototype.letAct = function(critter, vector) {
133+
var action = critter.act(new View(this, vector));
134+
if (action && action.type == "move") {
135+
var dest = this.checkDestination(action, vector);
136+
if (dest && this.grid.get(dest) == null) {
137+
this.grid.set(vector, null);
138+
this.grid.set(dest, critter);
139+
}
140+
}
141+
};
142+
143+
World.prototype.checkDestination = function(action, vector) {
144+
if (directions.hasOwnProperty(action.direction)) {
145+
var dest = vector.plus(directions[action.direction]);
146+
if (this.grid.isInside(dest))
147+
return dest;
148+
}
149+
};
150+
151+
function View(world, vector) {
152+
this.world = world;
153+
this.vector = vector;
154+
}
155+
156+
View.prototype.look = function(dir) {
157+
var target = this.vector.plus(directions[dir]);
158+
if (this.world.grid.isInside(target))
159+
return charFromElement(this.world.grid.get(target));
160+
else
161+
return "#";
162+
};
163+
164+
View.prototype.findAll = function(ch) {
165+
var found = [];
166+
for (var dir in directions)
167+
if (this.look(dir) == ch)
168+
found.push(dir);
169+
return found;
170+
};
171+
172+
View.prototype.find = function(ch) {
173+
var found = this.findAll(ch);
174+
if (found.length == 0) return null;
175+
return randomElement(found);
176+
};
177+
178+
for (var i = 0; i < 5; i++) {
179+
world.turn();
180+
console.log(world.toString());
181+
}
182+
183+
//animateWorld(world);

test.html

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,22 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<script>
5-
function f1(number){
6-
if(number == 0 || number == 1) return 1;
7-
return f1(number - 1) + f1(number - 2);
8-
}
9-
10-
function f2(number){
11-
if(number == 0 || number == 1) return 1;
12-
13-
var a2 = 1, a1 = 1, a;
14-
for(i = 2; i <= number; i++){
15-
a = a1 + a2;
16-
a2 = a1;
17-
a1 = a;
18-
}
19-
20-
return a;
21-
}
22-
23-
console.log(f1(6));
24-
console.log(f2(6));
25-
26-
</script>
4+
<title></title>
5+
<script src="./practice/Electronic_life_completejavascript.com.js">
6+
/*
7+
var test = {
8+
prop: 10,
9+
addPropTo: function(array) {
10+
return array.map(function(elt) {
11+
return this.prop + elt;
12+
}.bind(this));
13+
}
14+
};
15+
console.log(test.addPropTo([5]));
16+
*/
17+
</script>
2718
</head>
28-
<body></body>
29-
</html>
19+
<body>
20+
21+
</body>
22+
</html>

0 commit comments

Comments
 (0)