Skip to content

Commit 6aedea1

Browse files
committed
Add base for if condition
1 parent 412db0a commit 6aedea1

4 files changed

Lines changed: 72 additions & 1 deletion

File tree

‎SUMMARY.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@
1111
* [Strings](strings/README.md)
1212
* [Creation](strings/create.md)
1313
* [Concatenation](strings/concat.md)
14-
* [Length](strings/length.md)
14+
* [Length](strings/length.md)
15+
* [Conditional Logic](conditional/README.md)
16+
* [If](conditional/if.md)
17+
* [Else](conditional/else.md)
18+
* [Comparators](conditional/comparators.md)

‎comments/README.md‎

Whitespace-only changes.

‎conditional/README.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Conditional Logic
2+
3+
A condition is a test for something. Conditions are very important for programming, in several ways:
4+
5+
First of all conditions can be used to ensure that your program works, regardless of what data you throw at it for processing. If you blindly trust data, you’ll get into trouble and your programs will fail. If you test that the thing you want to do is possible and has all the required information in the right format, that won’t happen, and your program will be a lot more stable. Taking such precautions is also known as programming defensively.
6+
7+
The other thing conditions can do for you is allow for branching. You might have encountered branching diagrams before, for example when filling out a form. Basically, this refers to executing different “branches” (parts) of code, depending on if the condition is met or not.
8+
9+
In this chapter, we'll learn the base of conditional logic in Javascript.

‎conditional/if.md‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Condition If
2+
3+
The easiest condition is an if statement and its syntax is `if(condition){ do this … }`. The condition has to be true for the code inside the curly braces to be executed. You can for example test a string and set the value of another string dependent on its value:
4+
5+
```javascript
6+
var country = 'France';
7+
var weather;
8+
var food;
9+
var currency;
10+
11+
if(country === 'England'){
12+
weather = 'horrible';
13+
food = 'filling';
14+
currency = 'pound sterling';
15+
}
16+
17+
if(country === 'France'){
18+
weather = 'nice';
19+
food = 'stunning, but hardly ever vegetarian';
20+
currency = 'funny, small and colourful';
21+
}
22+
23+
if(country === 'Germany'){
24+
weather = 'average';
25+
food = 'wurst thing ever';
26+
currency = 'funny, small and colourful';
27+
}
28+
29+
var message = 'this is ' + country + ', the weather is ' +
30+
weather + ', the food is ' + food + ' and the ' +
31+
'currency is ' + currency;
32+
```
33+
34+
---
35+
36+
Fill up the value of `name` to validate the condition.
37+
38+
```js
39+
var name =
40+
41+
if (name == "John") {
42+
43+
}
44+
```
45+
46+
```js
47+
var name = "John";
48+
49+
if (name == "John") {
50+
51+
}
52+
```
53+
54+
```js
55+
assert(name == "John");
56+
```
57+
58+
---

0 commit comments

Comments
 (0)