Skip to content

Commit 18a034e

Browse files
committed
added a fundamental on conditional execution
1 parent 27d4c47 commit 18a034e

File tree

4 files changed

+189
-4
lines changed

4 files changed

+189
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Here you can find course content and homework for the JavaScript 1,2 and 3 modul
99
|0.|Preparation for your first JavaScript session|[Pre-reading](/Week0/README.md)|-|
1010
|1.|Git Session with Unmesh|[Reading Week 1](/Week1/README.md) | [Homework Week 1](/Week1/MAKEME.md)|
1111
|2.|• Intro JavaScript (What is it, where can you use it for)<br>• [Variables (var, let, const)](fundamentals/variables.md)<br>• [Basic Data types (strings, numbers, arrays, booleans)](fundamentals/values.md)<br>• [Operators](fundamentals/operators.md)<br>• [Naming conventions](fundamentals/naming_conventions.md)|[Reading Week 2](/Week2/README.md)|[Homework Week 2](/Week2/MAKEME.md)|
12-
|3.|• Git work flow :smiling_imp:<br>• [Advanced data types (objects)](fundamentals/objects.md) <br>• Conditions <br>• [Statements vs Expressions](fundamentals/statements_expressions.md)<br> • Loops (for/while)<br>• [Functions](fundamentals/functions.md) <br>• Scope|[Reading Week 3](/Week3/README.md)|[Homework Week 3](/Week3/MAKEME.md)|
12+
|3.|• Git work flow :smiling_imp:<br>• [Advanced data types (objects)](fundamentals/objects.md) <br>• [Conditional execution](fundamentals/conditional_execution.md) <br>• [Statements vs Expressions](fundamentals/statements_expressions.md)<br> • Loops (for/while)<br>• [Functions](fundamentals/functions.md) <br>• Scope|[Reading Week 3](/Week3/README.md)|[Homework Week 3](/Week3/MAKEME.md)|
1313
|4.|• Capturing user input <br>• Events<br>• [Basic DOM manipulations (img src, innerHTML)](fundamentals/DOM_manipulation.md)<br>• Code debugging using the browser <br>• [Code commenting](fundamentals/code_commenting.md)<br>• Structuring code files |[Reading Week 4](/Week4/README.md)|[Homework Week 4](/Week4/MAKEME.md)|
1414
|5.|• Functions + JSON/Arrays<br>• [Array Manipulations](fundamentals/array_manipulation.md)<br>• JSON<br>• [Map and filter](fundamentals/map_filter.md)<br>• Arrow functions |[Reading Week 5](/Week5/README.md)|[Homework Week 5](/Week5/MAKEME.md)|
1515
|6.|[Closures](fundamentals/scope_closures_this.md) <br>• Callbacks|[Reading Week 6](/Week6/README.md)|[Homework Week 6](/Week6/MAKEME.md)|

Week3/MAKEME.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Provide feedback to the homework of last week to one of your fellow students. Yo
3131
- Go through the topics of this week:
3232
- Git work flow
3333
- [Advanced data types (Objects)](../fundamentals/objects.md)
34-
- Conditions
34+
- [Conditional execution](../fundamentals/conditional_execution.md)
3535
- [Statements vs Expressions](../fundamentals/statements_expressions.md)
3636
- Loops (for/while)
3737
- [Functions](../fundamentals/functions.md)
@@ -187,7 +187,7 @@ Please make sure you REALLY understand the exercises below:
187187
188188
_Deadline Sunday morning_
189189
190-
Go trough the reading material in the [README.md](/Week2/README.md) to prepare for your next class
190+
Go through the reading material in the [README.md](/Week2/README.md) to prepare for your next class
191191
192192
```
193193
How to hand in your homework:

Week7/MAKEME.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Staff {
7373
// Initialize the objects
7474
// by pick your favorite movie from http://www.imdb.com/
7575
// and make sure that the following actions work.
76-
console.log(InstanceMovie.getStarts().map(actor => `${actor.getName()} ${actor.getAge}`));
76+
console.log(InstanceMovie.getStars().map(actor => `${actor.getName()} ${actor.getAge()}`));
7777
const director = InstanceMovie.getDirector();
7878
console.log(`Director: ${director.getName()}`);
7979
// Be creative with this let's see what you come up with :-)
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# Conditional execution
2+
3+
The normal order of execution of statements in a computer program is in straight-line order, from top to bottom. However, sometimes it is desirable to execute one or more statements _conditionally_, i.e. depending on whether some condition – determined by the state of your program – holds true.
4+
5+
## The `if` statement
6+
7+
In its simplest form the `if` statement looks like this:
8+
9+
```js
10+
if (condition) {
11+
// one or more statements that will be executed
12+
// if, and only if the condition holds true
13+
}
14+
```
15+
16+
Here, `condition` is a boolean expression that resolves to either `true` or `false` (or, more precisely, any expression that is 'truthy' or 'falsy', as will be explained later).
17+
18+
The statements within the curly braces `{` and `}` will be executed if the condition holds true, otherwise these statements will be skipped (i.e. ignored).
19+
20+
An example:
21+
22+
```js
23+
if (distance < 10) {
24+
console.log('I will take the bike.');
25+
}
26+
```
27+
28+
Here, the condition is the boolean expression `distance > 10`, which is either `true` or `false`.
29+
30+
It is also possible to add a statement block to be executed if (and only if) the condition does **not** hold true, using an `else` clause.
31+
32+
```js
33+
if (distance < 10) {
34+
console.log('I will take the bike.');
35+
} else {
36+
console.log('I will go by car.');
37+
}
38+
```
39+
40+
A condition can take more complex forms, using `&&` (logival AND) and `||` (logical OR) operators:
41+
42+
```js
43+
if (distance < 10 && !raining) {
44+
console.log('I will take the bike.');
45+
} else {
46+
console.log('I will go by car.');
47+
}
48+
```
49+
50+
In the example above `raining` is a boolean variable (either `true` or `false`), and the exclamation mark is the logical NOT operator that negates the boolean value (if it was `true` the result after negation is false and vice versa).
51+
52+
For more complex decisions you can concatenate multiple conditions using `else if` clauses.
53+
54+
```js
55+
if (distance < 1) {
56+
console.log('I will walk.');
57+
} else if (distance < 10) {
58+
console.log('I will take the bike.');
59+
} else if (distance < 50) {
60+
console.log('I will go by car.');
61+
} else {
62+
console.log('I will take the train.');
63+
}
64+
```
65+
66+
The statement block inside an `if`, `else` or `else if` may itself contain nested `if` statements, as in this example:
67+
68+
```js
69+
if (distance < 10) {
70+
if (raining) {
71+
console.log('I will go public transportation.');
72+
} else {
73+
console.log('I will walk.');
74+
}
75+
} else {
76+
console.log('I will go by car.');
77+
}
78+
```
79+
80+
> As (nested) `if` statements can become quite complex it is very important that you indent your source code so that there can be no confusion about which statement blocks are executed for each condition, as was done in the examples.
81+
82+
>More informaton on MDN: [if...else](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else)
83+
84+
## The conditional (ternary) operator
85+
86+
This operator can be used as a shortcut for an `if` statement when dealing with expressions.
87+
88+
The general format is:
89+
90+
```js
91+
condition ? expr1 : expr2
92+
```
93+
94+
('ternary' means: _composed of three parts_)
95+
96+
It is often used in combination with an assignment, as in this example:
97+
98+
```js
99+
const conditionOfCar = age < 1 ? 'new' : 'used';
100+
```
101+
102+
The `conditionOfCar` variable will be assigned the string `'new'` if the `age < 1` condition holds true, otherwise it is assigned the string `'used'`.
103+
104+
It is **not** recommended to use the conditional operator if you do not intend to use its value:
105+
106+
```js
107+
// Don't do this: it's yucky code
108+
age < 1 ? console.log('new') : console.log('used');
109+
```
110+
111+
>More informaton on MDN: [Conditional (ternary) Operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator)
112+
113+
## The switch statement
114+
115+
The `switch` statement can sometimes be a useful alternative to a concatenation of `if` statements. This is the case when the condition is an expression that can be decomposed into a number of distinct values or _cases_, as shown in the example below.
116+
117+
```js
118+
const hyfModule = 'JavaScript-1';
119+
120+
switch (hyfModule) {
121+
case 'HTML/CSS':
122+
console.log('In this module you will learn HTML and CSS.');
123+
break;
124+
case 'JavaScript-1':
125+
console.log('In this module you will learn Git and JavaScript basics.');
126+
break;
127+
case 'JavaScript-2':
128+
console.log('In this module you will learn about JavaScript in the browser with HTML and CSS.');
129+
break;
130+
case 'JavaScript-3':
131+
console.log('In this module you will learn about Async and API calls.');
132+
break;
133+
case 'Node':
134+
console.log('This module is about building server and CLI applications using Node.');
135+
break;
136+
case 'Database':
137+
console.log('In this module is about Relational and Non-Relational Data and Database Systems.');
138+
break;
139+
case 'React':
140+
console.log('In this module you will to build Single Page Applications using React.');
141+
break;
142+
case 'Project':
143+
console.log('In this final module you will do your graduation project.');
144+
break;
145+
default:
146+
console.log('This module is unknown: ' + hyfModule);
147+
}}
148+
```
149+
150+
Depending on the value of the expression specified in the `switch` clause, one of the `case` statement blocks is executed. Each statement block should end with a `break` statement to ensure that a `case` doesn't 'fall through' into the next `case`.
151+
152+
The `default` statement at the end is executed when none of the preceding cases hold true. The `default` statement is not strictly required, but is a best practice to always specify one.
153+
154+
>More informaton on MDN: [switch](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch)
155+
156+
## truthy, falsy
157+
158+
**truthy**: 'sort of' `true`
159+
**falsy**: 'sort of' `false`
160+
161+
From MDN:
162+
163+
In JavaScript, a **truthy** value is a value that is considered true when evaluated in a Boolean context. All values are truthy unless they are defined as **falsy**.
164+
165+
**falsy** values are:
166+
167+
- `false`
168+
- `0`
169+
- `""`
170+
- `null`
171+
- `undefined`
172+
- `NaN`
173+
174+
The example below will print `x is undefined` because `undefined` is **falsy**.
175+
176+
```js
177+
let x;
178+
if (x) {
179+
console.log('x is defined');
180+
} else {
181+
console.log('x is undefined');
182+
}
183+
```
184+
185+
>More informaton on MDN: [Truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy), [Falsy](https://developer.mozilla.org/en-US/docs/Glossary/Falsy)

0 commit comments

Comments
 (0)