content restructured

pull/16/head
Asabeneh 6 years ago
parent 0e8a4aa3be
commit d8e0fe2fe9

@ -74,14 +74,17 @@ Primitive data types are compared by its values. Let us compare different data v
```js
let numOne = 3
let numTwo = 3
console.log(numOne == numTwo) // true
let js = 'JavaScript'
let py = 'Python'
console.log(js == py) //false
let lightOn = true
let lightOff = false
console.log(lightOn == lightOff) // false
```
@ -93,6 +96,7 @@ Let us see by creating an array. An array is a list of data values in a square b
```js
let nums = [1, 2, 3]
nums[0] = 10
console.log(nums) // [10, 2, 3]
```
@ -101,6 +105,7 @@ As you can see, an array in which a non-primitive data type is mutable. Non-prim
```js
let nums = [1, 2, 3]
let numbers = [1, 2, 3]
console.log(nums == numbers) // false
let userOne = {
@ -108,11 +113,13 @@ name:'Asabeneh',
role:'teaching',
country:'Finland'
}
let userTwo = {
name:'Asabeneh',
role:'teaching',
country:'Finland'
}
console.log(userOne == userTwo)
```
@ -122,6 +129,7 @@ Non-primitive values are referred to as reference types because they are being c
```js
let nums = [1, 2, 3]
let numbers = nums
console.log(nums == numbers) // true
let userOne = {
@ -129,7 +137,9 @@ name:'Asabeneh',
role:'teaching',
country:'Finland'
}
let userTwo = userOne
console.log(userOne == userTwo) // true
```
@ -151,6 +161,7 @@ const PI = 3.14 // pi a geometrical constant
//More Examples
const boilingPoint = 100 // temperature in oC, boiling point of water which is a constant
const bodyTemp = 37 // oC average human body temperature, which is a constant
console.log(age, gravity, mass, PI, boilingPoint, bodyTemp)
```
@ -177,7 +188,8 @@ console.log(Math.max(-5, 3, 20, 4,5, 10)) // 20, returns the maximum value
const randNum = Math.random() // creates random number between 0 to 0.999999
console.log(randNum)
// Let create random number between 0 to 10
// Let us create random number between 0 to 10
const num = Math.floor(Math.random () * 11) // creates random number between 0 and 10
console.log(num)
@ -278,7 +290,9 @@ let language = 'JavaScript'
let job = 'teacher'
let age = 250
let fullName = firstName + space + lastName
let personInfoOne = fullName + '. I am ' + age + '. I live in ' + country; // ES5
console.log(personInfoOne)
```
@ -339,10 +353,10 @@ To create a template string, we use two backticks. We can inject data as express
**Example: 1**
```js
console.log(`The sum of 2 and 3 is 5`)
console.log(`The sum of 2 and 3 is 5`) // statically writing the data
let a = 2
let b = 3
console.log(`The sum of ${a} and ${b} is ${a + b}`)
console.log(`The sum of ${a} and ${b} is ${a + b}`) // injecting the data dynamically
```
**Example:2**
@ -389,9 +403,9 @@ Everything in JavaScript is an object. A string is a primitive data type that me
```js
let js = 'JavaScript'
console.log(js.length) // 10
console.log(js.length) // 10
let firstName = 'Asabeneh'
console.log(firstName.length) // 8
console.log(firstName.length) // 8
```
2. *Accessing characters in a string*: We can access each character in a string using its index. In programming, counting starts from 0. The first index of the string is zero, and the last index is one minus the length of the string.
@ -404,18 +418,18 @@ Let us access different characters in 'JavaScript' string.
let string = 'JavaScript'
let firstLetter = string[0]
console.log(firstLetter) // J
console.log(firstLetter) // J
let secondLetter = string[1] // a
let secondLetter = string[1] // a
let thirdLetter = string[2]
let lastLetter = string[9]
console.log(lastLetter) // t
console.log(lastLetter) // t
let lastIndex = string.length - 1
console.log(lastIndex) // 9
console.log(string[lastIndex]) // t
console.log(string[lastIndex]) // t
```
1. *toUpperCase()*: this method changes the string to uppercase letters.

@ -1,7 +1,6 @@
## Table of Contents
[<< Day 2](https://github.com/Asabeneh/30DaysOfJavaScript/blob/master/02_Day/02_day_data_types.md) | [Day 4 >>](https://github.com/Asabeneh/30DaysOfJavaScript/blob/master/04_Day/04_day_conditionals.md)
--
[<< Day 2](https://github.com/Asabeneh/30DaysOfJavaScript/blob/master/02_Day/02_day_data_types.md) | [Day 4 >>](https://github.com/Asabeneh/30DaysOfJavaScript/blob/master/04_Day/04_day_conditionals.md)
![Thirty Days Of JavaScript](./day_1_3.png)
@ -47,17 +46,17 @@
## Booleans
A boolean data type represents one of the two values:_true_ or _false_. Boolean value is either true or false. The use of these data types will be clear when you start the comparison operator. Any comparisons return a boolean value which is either true or false.
A boolean data type represents one of the two values:_true_ or _false_. Boolean value is either true or false. The use of these data types will be clear when you start the comparison operator. Any comparisons return a boolean value which is either true or false.
**Example: Boolean Values**
```js
let isLightOn = true;
let isRaining = false;
let isHungry = false;
let isMarried = true;
let truValue = 4 > 3 // true
let falseValue = 3 < 4 // false
let isLightOn = true
let isRaining = false
let isHungry = false
let isMarried = true
let truValue = 4 > 3 // true
let falseValue = 3 < 4 // false
```
We agreed that boolean values are either true or false.
@ -67,7 +66,7 @@ We agreed that boolean values are either true or false.
- All numbers(positive and negative) are truthy except zero
- All strings are truthy
- The boolean true
### Falsy values
- 0
@ -85,15 +84,15 @@ It is good to remember those truthy values and falsy values. In later section, w
If we declare a variable and if we do not assign a value, the value will be undefined. In addition to this, if a function is not returning the value will be undefined.
```js
let firstName;
console.log(firstName); //not defined, because it is not assigned to a value yet
let firstName
console.log(firstName) //not defined, because it is not assigned to a value yet
```
## Null
```js
let empty = null;
console.log(empty); // -> null , means no value
let empty = null
console.log(empty) // -> null , means no value
```
## Operators
@ -117,36 +116,38 @@ Arithmetic operators are mathematical operators.
- Addition(+): a + b
- Subtraction(-): a - b
- Multiplication(*): a * b
- Multiplication(_): a _ b
- Division(/): a / b
- Modulus(%):a % b
- Exponential(**):a ** b
```js
let numOne = 4;
let numTwo = 3;
let sum = numOne + numTwo;
let diff = numOne - numTwo;
let mult = numOne * numTwo;
let div = numOne / numTwo;
let remainder = numOne % numTwo;
let numOne = 4
let numTwo = 3
let sum = numOne + numTwo
let diff = numOne - numTwo
let mult = numOne * numTwo
let div = numOne / numTwo
let remainder = numOne % numTwo
let powerOf = numOne ** numTwo
console.log(sum, diff, mult, div, remainder, powerOf); // ->7,1,12,1.33,1, 64
let PI = 3.14;
let radius = 100; // length in meter
console.log(sum, diff, mult, div, remainder, powerOf) // 7,1,12,1.33,1, 64
const gravity = 9.81; // in m/s2
let mass = 72; // in Kilogram
const boilingPoint = 100; // temperature in oC, boiling point of water
const bodyTemp = 37; // body temperature in oC
let PI = 3.14
let radius = 100 // length in meter
const gravity = 9.81 // in m/s2
let mass = 72 // in Kilogram
const boilingPoint = 100 // temperature in oC, boiling point of water
const bodyTemp = 37 // body temperature in oC
//Let us calculate area of a circle
const areaOfCircle = PI * radius * radius;
console.log(areaOfCircle); // -> 314 m
const areaOfCircle = PI * radius * radius
console.log(areaOfCircle) // 314 m
// Let us calculate weight of an object
const weight = mass * gravity;
console.log(weight); // -> 706.32 N(Newton)
const weight = mass * gravity
console.log(weight) // 706.32 N(Newton)
//Concatenating string with numbers using string interpolation
/*
@ -156,7 +157,7 @@ console.log(weight); // -> 706.32 N(Newton)
*/
console.log(
`The boiling point of water is ${boilingPoint} oC.\nHuman body temperature is ${bodyTemp} oC.\nThe gravity of earth is ${gravity} m / s2.`
);
)
```
### Comparison Operators
@ -186,14 +187,13 @@ console.log(0 == ' ') // true, equivalent
console.log(0 === '') // false, not exactly the same
console.log(0 === false) // false, not exactly the same
console.log(1 == true) // true, equivalent
console.log(1 === true ) // false, not exactly the same
console.log(1 === true) // false, not exactly the same
console.log(undefined == null) // true
console.log(undefined === null) // true
console.log(NaN == NaN) // false, not equal
console.log(NaN === NaN) // false
console.log(typeof NaN) // number
console.log('mango'.length == 'avocado'.length) // false
console.log('mango'.length != 'avocado'.length) // true
console.log('mango'.length < 'avocado'.length) // true
@ -215,18 +215,17 @@ The following symbols are the common logical operators:
! negates true to false, false to true.
```js
//&& ampersand operator example
const check = 4 > 3 && 10 > 5 // true and true -> true
const check = 4 > 3 && 10 < 5 // true and false -> false
const check = 4 < 3 && 10 < 5 // false and false -> false
const check = 4 > 3 && 10 > 5 // true and true -> true
const check = 4 > 3 && 10 < 5 // true and false -> false
const check = 4 < 3 && 10 < 5 // false and false -> false
//|| pipe or operator, example
const check = 4 > 3 || 10 > 5 // true and true -> true
const check = 4 > 3 || 10 < 5 // true and false -> true
const check = 4 < 3 || 10 < 5 // false and false -> false
const check = 4 > 3 || 10 > 5 // true and true -> true
const check = 4 > 3 || 10 < 5 // true and false -> true
const check = 4 < 3 || 10 < 5 // false and false -> false
//! Negation examples
@ -245,16 +244,16 @@ In JavaScrip we use the increment operator to increase a value stored in a varia
```js
let count = 0
console.log(++count) // 1
console.log(count) // 1
console.log(++count) // 1
console.log(count) // 1
```
1. Post-increment
```js
let count = 0
console.log(count++) // 0
console.log(count) // 1
console.log(count++) // 0
console.log(count) // 1
```
We use most of the time post-increment. At leas you should remember how to use post-increment operator.
@ -267,16 +266,16 @@ In JavaScrip we use the decrement operator to decrease a value stored in a varia
```js
let count = 0
console.log(--count) // -1
console.log(count) // -1
console.log(--count) // -1
console.log(count) // -1
```
2. Post-decrement
```js
let count = 0
console.log(count--) // 0
console.log(count) // -1
console.log(count--) // 0
console.log(count) // -1
```
#### Ternary Operators
@ -326,7 +325,7 @@ I would like to recommend you to read about operator precendence from this [link
### Window alert() method
As you have seen at very beginning alert() method displays an alert box with a specified message and an OK button. It is a builtin method and it takes on argument.
As you have seen at very beginning alert() method displays an alert box with a specified message and an OK button. It is a builtin method and it takes on argument.
```js
alert(message)
@ -340,7 +339,7 @@ Do not use too much alert because it is destructing and annoying, use it just fo
### Window prompt() method
The window prompt methods display a prompt box with an input on your browser to take input values and the input data can be stored in a variable. The prompt() method takes two arguments. The second argument is optional.
The window prompt methods display a prompt box with an input on your browser to take input values and the input data can be stored in a variable. The prompt() method takes two arguments. The second argument is optional.
```js
prompt('required text', 'optional text')
@ -376,7 +375,7 @@ _getFullYear(), getMonths(), getDate(), getDay(), getHours(), getMinutes, getSec
Once we create time object. The time object will provide information about time. Let us create a time object
```js
const now = new Date ()
const now = new Date()
console.log(now) // Sat Jan 04 2020 00:56:41 GMT+0200 (Eastern European Standard Time)
```
@ -384,10 +383,10 @@ We have created a time object and we can access any date time information from t
### Getting full year
Let's extract or get the full from a time object.
Let's extract or get the full from a time object.
```js
const now = new Date ()
const now = new Date()
console.log(now.getFullYear()) // 2020
```
@ -396,7 +395,7 @@ console.log(now.getFullYear()) // 2020
Let's extract or get the month from a time object.
```js
const now = new Date ()
const now = new Date()
console.log(now.getMonth()) // 0, because the month is January, month(0-11)
```
@ -405,7 +404,7 @@ console.log(now.getMonth()) // 0, because the month is January, month(0-11)
Let's extract or get the date of the month from a time object.
```js
const now = new Date ()
const now = new Date()
console.log(now.getDate()) // 4, because the day of the month is 4th, day(0-31)
```
@ -414,8 +413,8 @@ console.log(now.getDate()) // 4, because the day of the month is 4th, day(0-31)
Let's extract or get the day of the week from a time object.
```js
const now = new Date ()
console.log(now.getDay()) // 6, because the day is Saturday which is the 5th day,
const now = new Date()
console.log(now.getDay()) // 6, because the day is Saturday which is the 5th day,
// Getting the weekday as a number (0-6)
```
@ -424,7 +423,7 @@ console.log(now.getDay()) // 6, because the day is Saturday which is the 5th day
Let's extract or get the hours from a time object.
```js
const now = new Date ()
const now = new Date()
console.log(now.getHours()) // 0, because the time is 00:56:41
```
@ -433,7 +432,7 @@ console.log(now.getHours()) // 0, because the time is 00:56:41
Let's extract or get the minutes from a time object.
```js
const now = new Date ()
const now = new Date()
console.log(now.getMinutes()) // 56, because the time is 00:56:41
```
@ -442,7 +441,7 @@ console.log(now.getMinutes()) // 56, because the time is 00:56:41
Let's extract or get the seconds from a time object.
```js
const now = new Date ()
const now = new Date()
console.log(now.getSeconds()) // 41, because the time is 00:56:41
```
@ -450,19 +449,19 @@ console.log(now.getSeconds()) // 41, because the time is 00:56:41
This method give time in milliseconds starting from January 1, 1970. It is also know as Unix time. We can get the unix time in two ways:
1. Using *getTime()*
1. Using _getTime()_
```js
const now = new Date () //
const now = new Date() //
console.log(now.getTime()) // 1578092201341, this is the number of seconds passed from January 1, 1970 to January 4, 2020 00:56:41
```
1. Using *Date.now()*
1. Using _Date.now()_
```js
const allSeconds = Date.now () //
const allSeconds = Date.now() //
console.log(allSeconds) // 1578092201341, this is the number of seconds passed from January 1, 1970 to January 4, 2020 00:56:41
const timeInSeconds = new Date ().getTime()
const timeInSeconds = new Date().getTime()
console.log(allSeconds == timeInSeconds) // true
```
@ -470,16 +469,16 @@ Let us format these values to a human readable time format.
**Example:**
```js
const now = new Date ();
const year = now.getFullYear(); // return year
const month = now.getMonth() + 1; // return month(0 - 11)
const date = now.getDate(); // return date (1 - 31)
const hours = now.getHours(); // return number (0 - 23)
const minutes = now.getMinutes();// return number (0 -59)
const now = new Date()
const year = now.getFullYear() // return year
const month = now.getMonth() + 1 // return month(0 - 11)
const date = now.getDate() // return date (1 - 31)
const hours = now.getHours() // return number (0 - 23)
const minutes = now.getMinutes() // return number (0 -59)
console.log(`${date}/${month}/${year} ${hours}:${minutes}`) // 4/1/2020 0:56
```
🌕 You have boundless energy. You have just completed day 3 challenge and you are three steps a head in to your way to greatness. Now do some exercises for your brain and for your muscle.
🌕 You have boundless energy. You have just completed day 3 challenge and you are three steps a head in to your way to greatness. Now do some exercises for your brain and for your muscle.
# 💻 Day 3: Exercises
@ -492,43 +491,43 @@ console.log(`${date}/${month}/${year} ${hours}:${minutes}`) // 4/1/2020 0:56
## 2. Exercises: Arithmetic Operators Part
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).
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).
```sh
Enter base: 20
Enter height: 10
The area of the triangle is 50
```
```sh
Enter base: 20
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?
```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.
```
```sh
Enter side a: 5
Enter side b: 4
Enter side c: 3
The perimeter of the triangle is 12
```
1. 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))
1. 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.
1. Calculate the slope, x-intercept and y-intercept of y = 2x -2
1. Slope is (m = y2-y1/x2-x1). Find the slope between point (2, 2) and point(6,10)
1. Compare the slope of above two questions.
1. 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.
1. Writ a script that prompt a user to enters hours and rate per hour. Calculate pay of the person?
```sh
Enter hours: 40
Enter rate per hour: 28
Your weekly earning is 1120
```
1. 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
@ -539,7 +538,7 @@ Boolean value is either true or false.
## 4. Exercises: Comparison Operators
Figure out the result of the following comparison expression first without using console.log(). After you decide the result confirm it using console.log()
Figure out the result of the following comparison expression first without using console.log(). After you decide the result confirm it using console.log()
1. 4 > 3
1. 4 >= 3
@ -552,7 +551,7 @@ Figure out the result of the following comparison expression first without using
1. 4 != '4'
1. 4 == '4'
1. 4 === '4'
Find the length of python and jargon and make a falsy comparison statement.
Find the length of python and jargon and make a falsy comparison statement.
## 5. Exercises: Logical Operators
@ -616,7 +615,7 @@ You are 25. You are old enough to drive
1. What is the minutes now?
1. Find out the numbers of seconds elapsed from January 1, 1970 to now.
1. Create a human readable time format
1. YYY-MM-DD HH:mm:ss
1. YYY-MM-DD HH:mm:ss
2. DD-MM-YYYY HH:mm:ss
3. DD/MM/YYY HH:mm:ss

Loading…
Cancel
Save