pull/623/merge
Selinay 9 months ago committed by GitHub
commit 91b122c77e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -7,10 +7,11 @@
<img alt="Twitter Follow" src="https://img.shields.io/twitter/follow/asabeneh?style=social">
</a>
<sub>Author:
<a href="https://www.linkedin.com/in/asabeneh/" target="_blank">Asabeneh Yetayeh</a><br>
<small> January, 2020</small>
</sub>
<sub>Author:
<a href="https://www.linkedin.com/in/asabeneh/" target="_blank">Asabeneh Yetayeh</a><br>
<small> January, 2020</small>
</sub>
</div>
[<< Day 3](../03_Day_Booleans_operators_date/03_booleans_operators_date.md) | [Day 5 >>](../05_Day_Arrays/05_day_arrays.md)
@ -18,22 +19,22 @@
![Thirty Days Of JavaScript](../images/banners/day_1_4.png)
- [📔 Day 4](#-day-4)
- [Conditionals](#conditionals)
- [If](#if)
- [If Else](#if-else)
- [If Else if Else](#if--else-if-else)
- [Switch](#switch)
- [Ternary Operators](#ternary-operators)
- [💻 Exercises](#-exercises)
- [Exercises: Level 1](#exercises-level-1)
- [Exercises: Level 2](#exercises-level-2)
- [Exercises: Level 3](#exercises-level-3)
- [Conditionals](#conditionals)
- [If](#if)
- [If Else](#if-else)
- [If Else if Else](#if--else-if-else)
- [Switch](#switch)
- [Ternary Operators](#ternary-operators)
- [💻 Exercises](#-exercises)
- [Exercises: Level 1](#exercises-level-1)
- [Exercises: Level 2](#exercises-level-2)
- [Exercises: Level 3](#exercises-level-3)
# 📔 Day 4
## Conditionals
Conditional statements are used for make decisions based on different conditions.
Conditional statements are used for make decisions based on different conditions.
By default , statements in JavaScript script executed sequentially from top to bottom. If the processing logic require so, the sequential flow of execution can be altered in two ways:
- Conditional execution: a block of one or more statements will be executed if a certain expression is true
@ -61,23 +62,23 @@ if (condition) {
**Example:**
```js
let num = 3
let num = 3;
if (num > 0) {
console.log(`${num} is a positive number`)
console.log(`${num} is a positive number`);
}
// 3 is a positive number
```
As you can see in the condition example above, 3 is greater than 0, so it is a positive number. The condition was true and the block of code was executed. However, if the condition is false, we won't see any results.
As you can see in the condition example above, 3 is greater than 0, so it is a positive number. The condition was true and the block of code was executed. However, if the condition is false, we won't see any results.
```js
let isRaining = true
let isRaining = true;
if (isRaining) {
console.log('Remember to take your rain coat.')
console.log('Remember to take your rain coat.');
}
```
The same goes for the second condition, if isRaining is false the if block will not be executed and we do not see any output. In order to see the result of a falsy condition, we should have another block, which is going to be _else_.
The same goes for the second condition, if isRaining is false the if block will not be executed and we do not see any output. In order to see the result of a falsy condition, we should have another block, which is going to be _else_.
### If Else
@ -93,155 +94,153 @@ if (condition) {
```
```js
let num = 3
let num = 3;
if (num > 0) {
console.log(`${num} is a positive number`)
console.log(`${num} is a positive number`);
} else {
console.log(`${num} is a negative number`)
console.log(`${num} is a negative number`);
}
// 3 is a positive number
num = -3
num = -3;
if (num > 0) {
console.log(`${num} is a positive number`)
console.log(`${num} is a positive number`);
} else {
console.log(`${num} is a negative number`)
console.log(`${num} is a negative number`);
}
// -3 is a negative number
```
```js
let isRaining = true
let isRaining = true;
if (isRaining) {
console.log('You need a rain coat.')
console.log('You need a rain coat.');
} else {
console.log('No need for a rain coat.')
console.log('No need for a rain coat.');
}
// You need a rain coat.
isRaining = false
isRaining = false;
if (isRaining) {
console.log('You need a rain coat.')
console.log('You need a rain coat.');
} else {
console.log('No need for a rain coat.')
console.log('No need for a rain coat.');
}
// No need for a rain coat.
```
The last condition is false, therefore the else block was executed. What if we have more than two conditions? In that case, we would use *else if* conditions.
The last condition is false, therefore the else block was executed. What if we have more than two conditions? In that case, we would use _else if_ conditions.
### If Else if Else
### If Else if Else
On our daily life, we make decisions on daily basis. We make decisions not by checking one or two conditions instead we make decisions based on multiple conditions. As similar to our daily life, programming is also full of conditions. We use *else if* when we have multiple conditions.
On our daily life, we make decisions on daily basis. We make decisions not by checking one or two conditions instead we make decisions based on multiple conditions. As similar to our daily life, programming is also full of conditions. We use _else if_ when we have multiple conditions.
```js
// syntax
if (condition) {
// code
// code
} else if (condition) {
// code
// code
} else {
// code
// code
}
```
**Example:**
```js
let a = 0
let a = 0;
if (a > 0) {
console.log(`${a} is a positive number`)
console.log(`${a} is a positive number`);
} else if (a < 0) {
console.log(`${a} is a negative number`)
console.log(`${a} is a negative number`);
} else if (a == 0) {
console.log(`${a} is zero`)
console.log(`${a} is zero`);
} else {
console.log(`${a} is not a number`)
console.log(`${a} is not a number`);
}
```
```js
// if else if else
let weather = 'sunny'
let weather = 'sunny';
if (weather === 'rainy') {
console.log('You need a rain coat.')
console.log('You need a rain coat.');
} else if (weather === 'cloudy') {
console.log('It might be cold, you need a jacket.')
console.log('It might be cold, you need a jacket.');
} else if (weather === 'sunny') {
console.log('Go out freely.')
console.log('Go out freely.');
} else {
console.log('No need for rain coat.')
console.log('No need for rain coat.');
}
```
### Switch
Switch is an alternative for **if else if else else**.
The switch statement starts with a *switch* keyword followed by a parenthesis and code block. Inside the code block we will have different cases. Case block runs if the value in the switch statement parenthesis matches with the case value. The break statement is to terminate execution so the code execution does not go down after the condition is satisfied. The default block runs if all the cases don't satisfy the condition.
Switch is an alternative for **if else if else else**.
The switch statement starts with a _switch_ keyword followed by a parenthesis and code block. Inside the code block we will have different cases. Case block runs if the value in the switch statement parenthesis matches with the case value. The break statement is to terminate execution so the code execution does not go down after the condition is satisfied. The default block runs if all the cases don't satisfy the condition.
```js
switch(caseValue){
switch (caseValue) {
case 1:
// code
break
break;
case 2:
// code
break
// code
break;
case 3:
// code
break
// code
break;
default:
// code
// code
}
```
```js
let weather = 'cloudy'
let weather = 'cloudy';
switch (weather) {
case 'rainy':
console.log('You need a rain coat.')
break
console.log('You need a rain coat.');
break;
case 'cloudy':
console.log('It might be cold, you need a jacket.')
break
console.log('It might be cold, you need a jacket.');
break;
case 'sunny':
console.log('Go out freely.')
break
console.log('Go out freely.');
break;
default:
console.log(' No need for rain coat.')
console.log(' No need for rain coat.');
}
// Switch More Examples
let dayUserInput = prompt('What day is today ?')
let day = dayUserInput.toLowerCase()
let dayUserInput = prompt('What day is today ?');
let day = dayUserInput.toLowerCase();
switch (day) {
case 'monday':
console.log('Today is Monday')
break
console.log('Today is Monday');
break;
case 'tuesday':
console.log('Today is Tuesday')
break
console.log('Today is Tuesday');
break;
case 'wednesday':
console.log('Today is Wednesday')
break
console.log('Today is Wednesday');
break;
case 'thursday':
console.log('Today is Thursday')
break
console.log('Today is Thursday');
break;
case 'friday':
console.log('Today is Friday')
break
console.log('Today is Friday');
break;
case 'saturday':
console.log('Today is Saturday')
break
console.log('Today is Saturday');
break;
case 'sunday':
console.log('Today is Sunday')
break
console.log('Today is Sunday');
break;
default:
console.log('It is not a week day.')
console.log('It is not a week day.');
}
```
// Examples to use conditions in the cases
@ -268,19 +267,19 @@ switch (true) {
Another way to write conditionals is using ternary operators. We have covered this in other sections, but we should also mention it here.
```js
let isRaining = true
let isRaining = true;
isRaining
? console.log('You need a rain coat.')
: console.log('No need for a rain coat.')
: console.log('No need for a rain coat.');
```
🌕 You are extraordinary and you have a remarkable potential. You have just completed day 4 challenges and you are four steps ahead to your way to greatness. Now do some exercises for your brain and muscle.
🌕 You are extraordinary and you have a remarkable potential. You have just completed day 4 challenges and you are four steps ahead to your way to greatness. Now do some exercises for your brain and muscle.
## 💻 Exercises
### Exercises: Level 1
1. Get user input using prompt(“Enter your age:”). If user is 18 or older , give feedback:'You are old enough to drive' but if not 18 give another feedback stating to wait for the number of years he needs to turn 18.
1. Get user input using prompt(“Enter your age:”). If user is 18 or older , give feedback:'You are old enough to drive' but if not 18 give another feedback stating to wait for the number of years he needs to turn 18.
```sh
Enter your age: 30
@ -299,46 +298,46 @@ isRaining
1. If a is greater than b return 'a is greater than b' else 'a is less than b'. Try to implement it in to ways
- using if else
- ternary operator.
- using if else
- ternary operator.
```js
let a = 4
let b = 3
```
```js
let a = 4;
let b = 3;
```
```sh
4 is greater than 3
```
```sh
4 is greater than 3
```
1. Even numbers are divisible by 2 and the remainder is zero. How do you check, if a number is even or not using JavaScript?
```sh
Enter a number: 2
2 is an even number
```sh
Enter a number: 2
2 is an even number
Enter a number: 9
9 is is an odd number.
```
Enter a number: 9
9 is is an odd number.
```
### Exercises: Level 2
1. Write a code which can give grades to students according to theirs scores:
1. Write a code which can give grades to students according to their scores:
- 80-100, A
- 70-89, B
- 70-79, B
- 60-69, C
- 50-59, D
- 0-49, F
1. Check if the season is Autumn, Winter, Spring or Summer.
If the user input is :
- September, October or November, the season is Autumn.
- December, January or February, the season is Winter.
- March, April or May, the season is Spring
- June, July or August, the season is Summer
- September, October, or November, the season is Autumn.
- December, January, or February, the season is Winter.
- March, April, or May, the season is Spring
- June, July, or August, the season is Summer
1. Check if a day is weekend day or a working day. Your script will take day as an input.
```sh
What is the day today? Saturday
What is the day today? Saturday
Saturday is a weekend.
What is the day today? saturDaY
@ -349,29 +348,28 @@ isRaining
What is the day today? FrIDAy
Friday is a working day.
```
```
### Exercises: Level 3
1. Write a program which tells the number of days in a month.
```sh
Enter a month: January
January has 31 days.
```sh
Enter a month: January
January has 31 days.
Enter a month: JANUARY
January has 31 day
Enter a month: JANUARY
January has 31 day
Enter a month: February
February has 28 days.
Enter a month: February
February has 28 days.
Enter a month: FEbruary
February has 28 days.
```
Enter a month: FEbruary
February has 28 days.
```
1. Write a program which tells the number of days in a month, now consider leap year.
🎉 CONGRATULATIONS ! 🎉
[<< Day 3](../03_Day_Booleans_operators_date/03_booleans_operators_date.md) | [Day 5 >>](../05_Day_Arrays/05_day_arrays.md)

Loading…
Cancel
Save