forked from liammclennan/JavaScript-Koans
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabout_control_structures.js
More file actions
73 lines (65 loc) · 1.6 KB
/
about_control_structures.js
File metadata and controls
73 lines (65 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
module("About Control Structures (topics/about_control_structures.js)");
test("if", function() {
var isPositive = false;
if (2 > 0) {
isPositive = true;
}
equal(__, isPositive, 'what is the value of isPositive?');
});
test("for", function() {
var counter = 10;
for (var i = 1; i <= 3; i++) {
counter = counter + i;
}
equal(__, counter, 'what is the value of counter?');
});
test("for in", function() {
// this syntax will be explained in about objects
var person = {
name: "Amory Blaine",
age: 102
};
var result = "";
// for in enumerates the property names of an object
for (var property_name in person) {
result = result + property_name;
};
equal(__, result, 'what is the value of result?');
});
test("ternary operator", function() {
var fruit = true ? "apple" : "orange";
equal(__, fruit, 'what is the value of fruit?');
fruit = false ? "apple" : "orange";
equal(__, fruit, 'now what is the value of fruit?');
});
test("switch", function() {
var result = 0;
switch (2) {
case 1:
result = 1;
break;
case 1+1:
result = 2;
break;
}
equal(__, result, 'what is the value of result?');
});
test("switch default case", function() {
var result = "Pippin";
switch ("m") {
case "f":
result = "Frodo";
break;
case "s":
result = "Samwise";
break;
default:
result = "Merry";
break;
}
equal(__, result, 'what is the value of result?');
});
test("null coalescing", function() {
var result = null || "a value";
equal(__, result, 'what is the value of result?');
});