pull/16/head
Asabeneh 5 years ago
parent 052c896004
commit 10347cfe07

@ -789,10 +789,22 @@ The quote 'There is no exercise better for the heart than reaching down and lift
"Love is not patronizing and charity isn't about pity, it is about love. Charity and love are the same -- with charity you give love, so don't just give money but reach out your hand instead." "Love is not patronizing and charity isn't about pity, it is about love. Charity and love are the same -- with charity you give love, so don't just give money but reach out your hand instead."
``` ```
32. Generate a random number between 0 and 100 inclusive. 32.
33. Generate a random number between 50 and 100 inclusive. 33. Check if 'on' is found in both python and jargon
34. Generate a random number between 0 and 255 inclusive. 34. _I hope this course is not full of jargon_. Check if _jargon_ is in the sentence.
35. Access the 'JavaScript' string characters using a random number. 35. Generate a random number between 0 and 100 inclusive.
36. Generate a random number between 50 and 100 inclusive.
37. Generate a random number between 0 and 255 inclusive.
38. Access the 'JavaScript' string characters using a random number.
39. Use console.log() and escape characters to print the following pattern.
```js
1 1 1 1 1
2 1 2 4 8
3 1 3 9 27
4 1 4 16 64
5 1 5 25 125
```
🎉 CONGRATULATIONS ! 🎉 🎉 CONGRATULATIONS ! 🎉

@ -464,19 +464,49 @@ console.log(`${date}/${month}/${year} ${hours}:${minutes}`) // 4/1/2020 0:56
## 1. Exercises: Data types Part ## 1. Exercises: Data types Part
1. Declare firstName, lastName, country, city, age, isMarried, year variable and assign value to it 1. Declare firstName, lastName, country, city, age, isMarried, year variable and assign value to it
1. The JavaScript typeof operator uses to check different data types. Check the data type of each variables from question number 1. 1. The JavaScript typeof operator uses to check different data types. Check the data type of each variables from question number 1.
1. Check if type of '10' is equal to 10
1. Check if parseInt('9.8') is equal to 10
## 2. Exercises: Arithmetic Operators Part ## 2. Exercises: Arithmetic Operators Part
JavaScript arithmetic operators are addition(+), subtraction(-), multiplication(*), division(/), modulus(%), exponential(**), increment(++) and decrement(--). 1. Write a script that prompt the user to enter base and height of the triangle and calculate an area of a triangle (area = 0.5 x b x h).
```js ```sh
let operandOne = 4; Enter base: 20
let operandTwo = 3; Enter height: 10
``` The area of the triangle is 50
```
1. Write a script that prompt the user to enter side a, side b, and side c of the triangle and and calculate the perimeter of triangle (perimeter = a + b + c)
```sh
Enter side a: 5
Enter side b: 4
Enter side c: 3
The perimeter of the triangle is 12
```
2. Get length and width using prompt and calculate an area of rectangle (area = length x width and the perimeter of rectangle (perimeter = 2 x (length + width))
3. Get radius using prompt and calculate the area of a circle (area = pi x r x r) and circumference of a circle(c = 2 x pi x r) where pi = 3.14.
4. Calculate the slope, x-intercept and y-intercept of y = 2x -2
5. Slope is (m = y2-y1/x2-x1). Find the slope between point (2, 2) and point(6,10)
6. Compare the slope of above two questions.
7. Calculate the value of y (y = x^2 + 6x + 9). Try to use different x values and figure out at what x value y is 0.
8. Writ a script that prompt a user to enters hours and rate per hour. Calculate pay of the person?
Using the above operands apply different JavaScript arithmetic operations. ```sh
Enter hours: 40
Enter rate per hour: 28
Your weekly earning is 1120
```
9. Write a script that prompt the user to enter number of years. Calculate the number of seconds a person can live. Assume some one lives just hundred years
```sh
Enter number of yours you live: 100
You lived 3153600000 seconds.
```
## 3. Exercises: Booleans Part ## 3. Exercises: Booleans Part
@ -500,21 +530,22 @@ Figure out the result of the following comparison expression first without using
1. 4 != '4' 1. 4 != '4'
1. 4 == '4' 1. 4 == '4'
1. 4 === '4' 1. 4 === '4'
Find the length of python and jargon and make a falsy comparison statement.
## 5. Exercises: Logical Operators ## 5. Exercises: Logical Operators
Figure out the result of the following expressions first without using console.log(). After you decide the result confirm it by using console.log() 1. Figure out the result of the following expressions first without using console.log(). After you decide the result confirm it by using console.log()
1. 4 > 3 && 10 < 12
1. 4 > 3 && 10 < 12 2. 4 > 3 && 10 > 12
1. 4 > 3 && 10 > 12 3. 4 > 3 || 10 < 12
1. 4 > 3 || 10 < 12 4. 4 > 3 || 10 > 12
1. 4 > 3 || 10 > 12 5. !(4 > 3)
1. !(4 > 3) 6. !(4 < 3)
1. !(4 < 3) 7. !(false)
1. !(false) 8. !(4 > 3 && 10 < 12)
1. !(4 > 3 && 10 < 12) 9. !(4 > 3 && 10 > 12)
1. !(4 > 3 && 10 > 12) 10. !(4 === '4')
1. !(4 === '4') 2. There is no 'on' in both dragon and python
## 6 Ternary Operator ## 6 Ternary Operator
@ -546,7 +577,7 @@ Your first name, Asabeneh is longer than your family name, Yetayeh
1. Using prompt get the year the user was born and if the user is 18 or above allow the user to drive if not tell the user to wait a certain amount of years. 1. Using prompt get the year the user was born and if the user is 18 or above allow the user to drive if not tell the user to wait a certain amount of years.
```js ```sh
// if the age is 25 // if the age is 25
You are 25. You are old enough to drive You are 25. You are old enough to drive
// if the age is under 18 // if the age is under 18

@ -0,0 +1,312 @@
## Table of Contents
[<< Day 3](https://github.com/Asabeneh/30DaysOfJavaScript/blob/master/03_Day/03_day_booleans_operators_date.md) | [Day 5 >>](#)
--
![Thirty Days Of JavaScript](./day_1_4.png)
- [📔 Day 4](#%f0%9f%93%94-day-4)
- [Conditionals](#conditionals)
- [If](#if)
- [If Else](#if-else)
- [If else if else](#if-else-if-else)
- [Switch](#switch)
- [Ternary Operators](#ternary-operators)
- [💻 Exercise - 8 : Conditionals](#%f0%9f%92%bb-exercise---8--conditionals)
# 📔 Day 4
## Conditionals
Conditional statements are used to decision 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
- Repetitive execution: a block of one or more statements will be repetitively executed as long as a certain expression is true. In this section, we will cover _if_, _else_ , _else if_ statements. The comparison and logical operator we learned in the previous sections will be useful in here.
Conditions can be implementing using the following ways:
- if
- if else
- if else if else
- switch
- ternary operator
### If
In JavaScript and other programming languages the key word _if_ use to check if a condition is true and to execute the block code. To create an if condition, we need _if_ keyword, condition inside a parenthesis and block of code inside a curly bracket({}).
```js
// syntax
if (condition) {
//this part of code run for truthy condition
}
```
**Example:**
```js
let num = 3
if (num > 0) {
console.log(`${num} is a positive number`)
}
// 3 is a positive number
```
```js
let isRaining = true
if (isRaining) {
console.log('Remember to take your rain coat.')
}
```
As you can see in the above condition, 3 is greater than 0 and it is a positive number. The condition was true and the block code was executed. However, if the condition is false, we do not see a result. The same goes for the second condition, if isRaining is false the if block will not be executed and we do not see an output. In order to see the result of the falsy condition, we should have another block, which is going to be _else_.
### If Else
If condition is true the first block will be executed, if not the else condition will be executed.
```js
// syntax
if (condition) {
// this part of code run for truthy condition
} else {
// this part of code run for false condition
}
```
```js
let num = 3
if (num > 0) {
console.log(`${num} is a positive number`)
} else {
console.log(`${num} is a negative number`)
}
// 3 is a positive number
num = -3
if (num > 0) {
console.log(`${num} is a positive number`)
} else {
console.log(`${num} is a negative number`)
}
// -3 is a negative number
```
```js
let isRaining = true
if (isRaining) {
console.log('You need a rain coat.')
} else {
console.log('No need for a rain coat.')
}
// You need a rain coat.
isRaining = false
if (isRaining) {
console.log('You need a rain coat.')
} else {
console.log('No need for a rain coat.')
}
// No need for a rain coat.
```
The above condition is false, therefore the else block was executed. How about if our condition is more than two, we will use *ele if* conditions.
### If else if else
On our daily life, we make decision on daily basis. We make decision 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 conditions. We use *else if* when we have multiple conditions.
```js
// syntax
if (condition) {
// code
} else if (condition) {
// code
} else {
// code
}
```
**Example:**
```js
let a = 0
if (a > 0) {
console.log(`A${a} is a positive number`)
} else if (a < 0) {
print(`${a} is a negative number`)
else if (a == 0) {
print(` ${a} is zero`)
} else {
print('${a) is not a number')
}
```
```js
// if else if else
let weather = 'sunny'
if (weather === 'rainy') {
console.log('You need a rain coat.')
} else if (weather === 'cloudy') {
console.log('It might be cold, you need a jacket.')
} else if (weather === 'sunny') {
console.log('Go out freely.')
} else {
console.log('No need for rain coat.')
}
```
### Switch
Switch is an alternative for **if else if else else**
```js
let weather = 'cloudy'
switch (weather) {
case 'rainy':
console.log('You need a rain coat.')
break
case 'cloudy':
console.log('It might be cold, you need a jacket.')
break
case 'sunny':
console.log('Go out freely.')
break
default:
console.log(' No need for rain coat.')
}
// Switch More Examples
var dayUserInput = prompt('What day is today ?')
var day = dayUserInput.toLowerCase()
switch (day) {
case 'monday':
console.log('Today is Monday')
break
case 'tuesday':
console.log('Today is Tuesday')
break
case 'wednesday':
console.log('Today is Wednesday')
break
case 'thursday':
console.log('Today is Thursday')
break
case 'friday':
console.log('Today is Friday')
break
case 'saturday':
console.log('Today is Saturday')
break
case 'sunday':
console.log('Today is Sunday')
break
default:
console.log('It is not a week day.')
}
```
### Ternary Operators
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
isRaining
? console.log('You need a rain coat.')
: console.log('No need for a rain coat.')
```
🌕 You have remarkable. You have just completed day 4 challenge and you are four steps a head in to your way to greatness. Now do some exercises for your brain and for your muscle.
# 💻 Exercise - 8 : Conditionals
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 feedback to wait for the years he supposed to wait for.
```sh
Enter your age: 30
You are old enough to drive.
Enter your age:15
You are left with 3 years to drive.
```
1. Compare the values of myAge and yourAge using if … else. Based on the comparison log to console who is older (me or you). Use prompt(“Enter your age:”) to get the age as input.
```sh
Enter your age: 30
You are 5 years older than me.
```
1. If a is greater than b return a is greater than b else a is less than b. Do it both using if else and ternary operator.
```js
let a = 4
let b = 3
```
```sh
4 is greater than 3
```
1. Write a code which give grade students according to theirs scores:
- 80-100, A
- 70-89, 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
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
Enter a number 9
9 is is an odd number.
```
1. Check if a day is week end day or a working day. Your script will take day as an input.
```sh
What is the day is today? Saturday
Saturday is a weekend day.
What is the day today? saturDaY
Saturday is a weekend day.
What is the day today? Friday
Friday is a work day.
What is the day today? FrIDAy
Friday is a work day.
```
8. Write a program which tells the number days in a month.
```sh
Enter month: January
January has 31 days.
Enter month: JANUARY
January has 31 day
Enter month: February
February has 28 days.
Enter month: FEbruary
February has 28 days.
```
🎉 CONGRATULATIONS ! 🎉
[<< Day 3](https://github.com/Asabeneh/30DaysOfJavaScript/blob/master/03_Day/03_day_booleans_operators_date.md) | [Day 5 >>](#)

Binary file not shown.

After

Width:  |  Height:  |  Size: 75 KiB

Loading…
Cancel
Save