Skip to content

Commit fa2e6e0

Browse files
committed
added examples
1 parent 8b77754 commit fa2e6e0

18 files changed

Lines changed: 287 additions & 41 deletions

‎v4-cokapi/backends/javascript/tests/eloquentjs-4.2.golden‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{ code: 'function reverseArray(array) {\n var output = [];\n for (var i = array.length - 1; i >= 0; i--)\n output.push(array[i]);\n return output;\n}\n\nfunction reverseArrayInPlace(array) {\n for (var i = 0; i < Math.floor(array.length / 2); i++) {\n var old = array[i];\n array[i] = array[array.length - 1 - i];\n array[array.length - 1 - i] = old;\n }\n return array;\n}\n\nconsole.log(reverseArray(["A", "B", "C"]));\n// → ["C", "B", "A"];\nvar arrayValue = [1, 2, 3, 4, 5];\nreverseArrayInPlace(arrayValue);\nconsole.log(arrayValue);\n// → [5, 4, 3, 2, 1]',
1+
{ code: 'function reverseArray(array) {\n var output = [];\n for (var i = array.length - 1; i >= 0; i--)\n output.push(array[i]);\n return output;\n}\n\nfunction reverseArrayInPlace(array) {\n for (var i = 0; i < Math.floor(array.length / 2); i++) {\n var old = array[i];\n array[i] = array[array.length - 1 - i];\n array[array.length - 1 - i] = old;\n }\n return array;\n}\n\nconsole.log(reverseArray(["A", "B", "C"]));\n// → ["C", "B", "A"];\nvar arrayValue = [1, 2, 3, 4, 5];\nreverseArrayInPlace(arrayValue);\nconsole.log(arrayValue);\n// → [5, 4, 3, 2, 1]\n\n// Source: Eloquent JavaScript -- Exercise 4.2\n// http://eloquentjavascript.net/code/#4.2',
22
trace:
33
[ { stdout: '',
44
func_name: '',
@@ -869,8 +869,8 @@
869869
reverseArrayInPlace: [ 'REF', 2 ],
870870
arrayValue: [ 'REF', 5 ] },
871871
ordered_globals: [ 'reverseArray', 'reverseArrayInPlace', 'arrayValue' ],
872-
line: 22,
873-
col: 20,
872+
line: 25,
873+
col: 42,
874874
event: 'step_line',
875875
heap:
876876
{ '1':

‎v4-cokapi/backends/javascript/tests/eloquentjs-4.2.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ var arrayValue = [1, 2, 3, 4, 5];
2020
reverseArrayInPlace(arrayValue);
2121
console.log(arrayValue);
2222
// → [5, 4, 3, 2, 1]
23+
24+
// Source: Eloquent JavaScript -- Exercise 4.2
25+
// http://eloquentjavascript.net/code/#4.2
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
try {
2+
var x = 5;
3+
throw new Error("oh crapo");
4+
}
5+
catch (e) {
6+
console.log('hello');
7+
console.log('caught');
8+
}
9+
finally {
10+
console.log('done');
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var globalZ = 10;
2+
3+
function foo(y) {
4+
function bar(x) {
5+
globalZ += 100;
6+
return x + y + globalZ;
7+
}
8+
return bar;
9+
}
10+
11+
var b = foo(1);
12+
b(2);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function Vector(x, y) {
2+
this.x = x;
3+
this.y = y;
4+
}
5+
6+
Vector.prototype.plus = function(other) {
7+
return new Vector(this.x + other.x, this.y + other.y);
8+
};
9+
10+
var v1 = new Vector(1, 2);
11+
var v2 = Vector(20, 30); // whoops, forgot 'new'
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var intNum = 42;
2+
var floatNum = 3.14159;
3+
var nanNum = NaN;
4+
var infNum = Infinity;
5+
var ninfNum = -Infinity;
6+
7+
var str = "hello world";
8+
9+
var boolTrue = true;
10+
var boolFalse = false;
11+
12+
var nullVal = null;
13+
var undefVal = undefined;
14+
15+
var lst = ['a', 'b', 3, 4, 5, 'f'];
16+
17+
var obj = {name: 'John', age: 35, hasChildren: true};
18+
19+
var i = 5;
20+
var obj_lst = [i, {foo: i+1, poop: [1, 2, 3]}, {bar: i+2}];
21+
22+
obj.name = 'Jane';
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function fact(n) {
2+
if (n == 0) {
3+
return 1;
4+
}
5+
else {
6+
return n * fact(n-1);
7+
}
8+
}
9+
10+
fact(3);
11+
fact(3);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Adapted from Effective JavaScript
2+
function Actor(x, y) {
3+
this.x = x;
4+
this.y = y;
5+
}
6+
7+
Actor.prototype.moveTo = function(x, y) {
8+
this.x = x;
9+
this.y = y;
10+
}
11+
12+
function SpaceShip(x, y) {
13+
Actor.call(this, x, y);
14+
this.points = 0;
15+
}
16+
17+
SpaceShip.prototype = Object.create(Actor.prototype); // inherit!
18+
SpaceShip.prototype.type = "spaceship";
19+
SpaceShip.prototype.scorePoint = function() {
20+
this.points++;
21+
}
22+
23+
var s = new SpaceShip(10, 20);
24+
s.moveTo(30, 40);
25+
s.scorePoint();
26+
s.scorePoint();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var x = 10; // global
2+
function foo() {
3+
var x = 10;
4+
function bar() {
5+
var y = x;
6+
x *= 2;
7+
console.log(x, y);
8+
}
9+
return bar;
10+
}
11+
var b = foo();
12+
// x inside of bar should be the x in foo, *not* the global x
13+
b();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
x = [1, 2, 3]
2+
y = [4, 5, 6]
3+
z = y
4+
y = x
5+
x = z
6+
7+
x = [1, 2, 3] # a different [1, 2, 3] list!
8+
y = x
9+
x.append(4)
10+
y.append(5)
11+
z = [1, 2, 3, 4, 5] # a different list!
12+
x.append(6)
13+
y.append(7)
14+
y = "hello"
15+
16+
17+
def foo(lst):
18+
lst.append("hello")
19+
bar(lst)
20+
21+
def bar(myLst):
22+
print(myLst)
23+
24+
foo(x)
25+
foo(z)

0 commit comments

Comments
 (0)