Skip to content

Commit c318cbe

Browse files
committed
Merge pull request #33 from padolsey/patch1
Correct code tabbing in conditional-code.md
2 parents 7b61905 + de236e9 commit c318cbe

File tree

1 file changed

+54
-55
lines changed

1 file changed

+54
-55
lines changed

content/javascript-101/conditional-code.md

+54-55
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@ attribution: jQuery Fundamentals
77
Sometimes you only want to run a block of code under certain conditions. Flow control — via if and else blocks — lets you run code only under certain conditions.
88

99
<javascript caption="Flow control">
10-
var foo = true;
11-
var bar = false;
12-
13-
if (bar) {
14-
// this code will never run
15-
console.log('hello!');
16-
}
17-
18-
if (bar) {
19-
// this code won't run
10+
var foo = true;
11+
var bar = false;
12+
13+
if (bar) {
14+
// this code will never run
15+
console.log('hello!');
16+
}
17+
18+
if (bar) {
19+
// this code won't run
20+
} else {
21+
if (foo) {
22+
// this code will run
2023
} else {
21-
if (foo) {
22-
// this code will run
23-
} else {
24-
// this code would run if foo and bar were both false
25-
}
24+
// this code would run if foo and bar were both false
2625
}
26+
}
2727
</javascript>
2828

2929
<div class="note" markdown="1">
@@ -42,18 +42,18 @@ kinds of values are "truthy" and which kinds of values are "falsy." Sometimes,
4242
values that seem like they should evaluate one way actually evaluate another.
4343

4444
<javascript caption="Values that evaluate to true">
45-
'0';
46-
'any string';
47-
[]; // an empty array
48-
{}; // an empty object
49-
1; // any non-zero number
45+
'0';
46+
'any string';
47+
[]; // an empty array
48+
{}; // an empty object
49+
1; // any non-zero number
5050
</javascript>
5151

5252
<javascript caption="Values that evaluate to false">
53-
''; // an empty string
54-
NaN; // JavaScript's "not-a-number" variable
55-
null;
56-
undefined; // be careful -- undefined can be redefined!
53+
''; // an empty string
54+
NaN; // JavaScript's "not-a-number" variable
55+
null;
56+
undefined; // be careful -- undefined can be redefined!
5757
</javascript>
5858

5959
## Conditional Variable Assignment with The Ternary Operator
@@ -65,9 +65,9 @@ condition is true, it returns a certain value, otherwise it returns a different
6565
value.
6666

6767
<javascript caption="The ternary operator">
68-
// set foo to 1 if bar is true;
69-
// otherwise, set foo to 0
70-
var foo = bar ? 1 : 0;
68+
// set foo to 1 if bar is true;
69+
// otherwise, set foo to 0
70+
var foo = bar ? 1 : 0;
7171
</javascript>
7272

7373
While the ternary operator can be used without assigning the return value to a
@@ -81,48 +81,47 @@ at the value of a variable or expression, and run different blocks of code
8181
depending on the value.
8282

8383
<javascript caption="A switch statement">
84-
switch (foo) {
84+
switch (foo) {
8585

86-
case 'bar':
87-
alert('the value was bar -- yay!');
88-
break;
86+
case 'bar':
87+
alert('the value was bar -- yay!');
88+
break;
8989

90-
case 'baz':
91-
alert('boo baz :(');
92-
break;
90+
case 'baz':
91+
alert('boo baz :(');
92+
break;
9393

94-
default:
95-
alert('everything else is just ok');
96-
break;
94+
default:
95+
alert('everything else is just ok');
96+
break;
9797

98-
}
98+
}
9999
</javascript>
100100

101101
Switch statements have somewhat fallen out of favor in JavaScript, because
102102
often the same behavior can be accomplished by creating an object that has more
103103
potential for reuse, testing, etc. For example:
104104

105105
<javascript>
106+
var stuffToDo = {
107+
'bar' : function() {
108+
alert('the value was bar -- yay!');
109+
},
106110

107-
var stuffToDo = {
108-
'bar' : function() {
109-
alert('the value was bar -- yay!');
110-
},
111+
'baz' : function() {
112+
alert('boo baz :(');
113+
},
111114

112-
'baz' : function() {
113-
alert('boo baz :(');
114-
},
115-
116-
'default' : function() {
117-
alert('everything else is just ok');
118-
}
119-
};
120-
121-
if (stuffToDo[foo]) {
122-
stuffToDo[foo]();
123-
} else {
124-
stuffToDo['default']();
115+
'default' : function() {
116+
alert('everything else is just ok');
125117
}
118+
};
119+
120+
if (stuffToDo[foo]) {
121+
stuffToDo[foo]();
122+
} else {
123+
stuffToDo['default']();
124+
}
126125
</javascript>
127126

128127
We'll look at objects in greater depth later in this chapter.

0 commit comments

Comments
 (0)