Skip to content

Commit c71e334

Browse files
committed
content adde
1 parent f778ed1 commit c71e334

1 file changed

Lines changed: 157 additions & 14 deletions

File tree

readMe.md

Lines changed: 157 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ if (isRaining) {
488488
}
489489
```
490490

491-
#### If Else if else
491+
#### If else if else
492492

493493
```js
494494
// if else if else
@@ -867,10 +867,10 @@ Slice: To cut out a multiple items in range. It takes two paramters:starting and
867867
Splice: It takes three parameters:Starting position, number of times to be removed and number items to be added.
868868
869869
```js
870-
const numbers = [1,2,3,4,5];
870+
const numbers = [1, 2, 3, 4, 5];
871871
console.log(numbers.splice() // -> remove all items
872872
console.log(numbers.splice(0,1)) // remove the first item
873-
console.log(numbers.splice(3,3, 6, 7, 8)) // -> [1,2,6,7,8] //it removes two item and replace three items
873+
console.log(numbers.splice(3, 3, 6, 7, 8)) // -> [1,2,6,7,8] //it removes two item and replace three items
874874

875875
```
876876
@@ -911,6 +911,29 @@ console.log(numbers); // -> [0,1,2,3,4,5]
911911
---
912912
913913
#### Exercise -10 : Array Methods
914+
```js
915+
const shoppingCart = ['Milk','Coffee','Tea', 'Honey'];
916+
const todoList = [
917+
{
918+
task:'Learn JavaScript',
919+
time:'4/3/2019 8:30',
920+
completed:true
921+
922+
},
923+
{
924+
task:'Help some in need',
925+
time:'4/3/2019 4:00',
926+
completed:false
927+
928+
},
929+
{
930+
task:'Do some physical exercises',
931+
time:'4/3/2019 6:00',
932+
completed:false
933+
934+
}]
935+
936+
```
914937
915938
## Functions
916939
@@ -1145,14 +1168,14 @@ const square = n => n * n; // -> 4
11451168
4. Write a function ***convertRgbToHexa*** which converts rgb to hexa color and it returns an hexa color.
11461169
5. Write a function ***generateColors*** which can generate any number of hexa or rgb colors.
11471170
```js
1148-
generate('hexa', 3)
1171+
generateColors('hexa', 3)
11491172
['#a3e12f','#03ed55','#eb3d2b']
1150-
generate('hexa', 1)
1173+
generateColors('hexa', 1)
11511174
'#b334ef'
11521175

1153-
generate('rgb', 3)
1176+
generateColors('rgb', 3)
11541177
['rgb(5, 55, 175','rgb(50, 105, 100','rgb(15, 26, 80']
1155-
generate('rgb', 1)
1178+
generateColors('rgb', 1)
11561179
'rgb(33,79, 176)'
11571180

11581181
```
@@ -1174,11 +1197,13 @@ const square = n => n * n; // -> 4
11741197
13. Write a function called *isPrime*, which checks if a number is prime number.
11751198
14. Write a functions which checks if all items are unique in the array.
11761199
15. Write a function which checks if all the itmes of the array are the same data type.
1200+
1. JavaScript variable name does not support special characters or symbols execpt $ or _. Write a function ***isValidVariable** which check if a variable is valid or invlaid variable.
11771201
16. Write a function which returns array of seven random numbers in a range of 0-9. All the numbers must be unique.
11781202
```js
11791203
sevenRandomNumbers()[(1, 4, 5, 7, 9, 8, 0)];
11801204
```
11811205
1206+
11821207
## Object
11831208
11841209
Everything can be an object and objects do have properties and properties have values.
@@ -1460,8 +1485,8 @@ const countriesToUpperCase = countries.map(country => country.toUpperCase());
14601485
//Filter countries containing land
14611486
const countriesContainingLand = countries.filter(country => country.includes('land'));
14621487
console.log(countriesContainingLand ) //["Finland", "Ireland"]
1463-
const countriesEndByia = countries.filter(country => country.includes('land'));
1464-
console.log(countriesEndByia) //["Finland", "Ireland"]
1488+
const countriesEndByia = countries.filter(country => country.includes('ia'));
1489+
console.log(countriesEndByia) //["Albania", "Bolivia","Ethiopia"]
14651490
const countriesHaveFiveLetters = countries.filter(country => country.length === 5);
14661491
console.log(countriesHaveFiveLetters ) //  ["Japan", "Kenya"]
14671492
const scores = [{name:'Asabeneh', score:95},{name:'Mathias', score:80},{name:'Elias', score:50},{name:'Martha', score:85},{name:'John', score:100}];
@@ -1561,7 +1586,7 @@ Destructuring is a way to unpack arrays, and objects and assigning to a distinct
15611586
let [numOne, numTwo, numThree] = numbers;
15621587
console.log(numOne, numTwo, numThree) // 1,2,3
15631588
const names = ['Asabeneh', 'Brook', 'David', 'John']
1564-
let [firstPerson, secondPerson, ThirdPerson, fourth Person] = name;s
1589+
let [firstPerson, secondPerson, ThirdPerson, fourth Person] = names;
15651590
console.log(firstName, secondPerson,thirdPerson, fourthPerson) //Asabeneh, Brook, David, John
15661591
const scientificConstants = [2.72, 3.14, 9.81, 37, 100];
15671592
let [e, pi, gravity, bodyTemp, boilingTemp] = scientificConstants
@@ -1578,6 +1603,12 @@ If we like to skip on of the values in the array we use additional comma. The co
15781603
console.log(secondPerson, fourthPerson) //Brook, John
15791604

15801605
```
1606+
We can use default value in case the value of array for that index is undefined:
1607+
```js
1608+
const names = [undefined, 'Brook', 'David'];
1609+
let [firstPerson = 'Asabeneh', secondPerson, thirdPerson, fourthPerson = 'John' ] = names;
1610+
console.log(firstPerson, secondPerson, thirdPerson, fourthPerson) // Asabeneh Brook David John
1611+
```
15811612
15821613
#### Destructuring Object
15831614
When we destructure the name of the variable we use to destructure should be exactly the same us the key or property of the object. See example below.
@@ -1588,7 +1619,7 @@ const rectangle = {
15881619
area: 200
15891620
}
15901621
let {width, height, area, perimeter} = rectangle;
1591-
console.log(width, height, area, perimeter) //20, 10, 200, undefined
1622+
console.log(width, height, area, perimeter) //20 10 200 undefined
15921623

15931624
```
15941625
#### Renaming during structuring
@@ -1599,11 +1630,122 @@ const rectangle = {
15991630
area: 200
16001631
}
16011632
let {width:w, heigh:h, area:a, perimeter:p} = rectangle;
1602-
console.log(w, h, a, p) //20, 10, 200, undefined
1633+
console.log(w, h, a, p) //20 10 200 undefined
1634+
1635+
```
1636+
If the key is not found in the object the variable will be assinged to undefined. In case, the key is not in the object we can give a default value during declaration. See the example.
1637+
```js
1638+
const rectangle = {
1639+
width: 20,
1640+
height:10,
1641+
area: 200
1642+
}
1643+
let {width, heigh, area, perimeter = 60} = rectangle;
1644+
console.log(width, height, area, perimeter) //20 10 200 60
1645+
//Lets modify the object:width to 30 and perimeter to 80
1646+
const rectangle = {
1647+
width: 30,
1648+
height:10,
1649+
area: 200,
1650+
perimeter:80
1651+
}
1652+
let {width, heigh, area, perimeter = 60} = rectangle;
1653+
console.log(width, height, area, perimeter) //20 10 200 80
1654+
```
1655+
1656+
Destructuring keys as a function paramters. Lets create a function which take a rectangle object and it return a perimeter of a rectangle.
1657+
```js
1658+
// Without destructuring
1659+
const rect = {
1660+
width:20,
1661+
height:10
1662+
}
1663+
const calculatePerimeter = (rectangle) => {
1664+
return 2 * (rectangle.width + rectangle.height)
1665+
}
1666+
console.log(calculatePerimeter(rect)) // 60
1667+
//with destructuring
1668+
1669+
const calculatePerimeter = ({width, height}) => {
1670+
return 2 * (width + height)
1671+
}
16031672

1673+
console.log(calculatePerimeter(rect)) // 60
1674+
1675+
//Another Example
1676+
const person = {
1677+
firstName: "Asabeneh",
1678+
lastName: "Yetayeh",
1679+
age: 200,
1680+
country: "Finland",
1681+
job: "Instructor and Developer",
1682+
skills: [
1683+
"HTML",
1684+
"CSS",
1685+
"JavaScript",
1686+
"React",
1687+
"Redux",
1688+
"Node",
1689+
"MongoDB",
1690+
"Python",
1691+
"D3.js"
1692+
],
1693+
languages: ["Amharic", "English", "Suomi(Finnish)"]
1694+
};
1695+
// Lets create a function which give information about the person object without destructuring
1696+
1697+
const getPersonInfo = obj => {
1698+
const skills = obj.skills;
1699+
const formattedSkills = skills.slice(0, -1).join(", ");
1700+
const languages = obj.languages;
1701+
const formattedLanguages = languages.slice(0, -1).join(", ");
1702+
1703+
return `${obj.firstName} ${obj.lastName} lives in ${obj.country}. He is ${
1704+
obj.age
1705+
} years old. He is an ${obj.job}. He teaches ${formattedSkills} and ${
1706+
skills[skills.length - 1]
1707+
}. He speakes ${formattedLanguages} and a little bit of ${languages[2]}.`;
1708+
};
1709+
console.log(getPersonInfo(person));
1710+
// Lets create a function which give information about the person object with destructuring
1711+
1712+
const getPersonInfo = ({
1713+
firstName,
1714+
lastName,
1715+
age,
1716+
country,
1717+
job,
1718+
skills,
1719+
languages
1720+
}) => {
1721+
const formattedSkills = skills.slice(0, -1).join(", ");
1722+
const formattedLanguages = languages.slice(0, -1).join(", ");
1723+
1724+
return `${firstName} ${lastName} lives in ${country}. He is ${age} years old. He is an ${job}. He teaches ${formattedSkills} and ${skills[skills.length - 1]}. He speakes ${formattedLanguages} and a little bit of ${languages[2]}.`;
1725+
};
1726+
console.log(getPersonInfo(person))
1727+
/*
1728+
Asabeneh Yetayeh lives in Finland. He is 200 years old. He is an Instructor and Developer. He teaches HTML, CSS, JavaScript, React, Redux, Node, MongoDB, Python and D3.js. He speakes Amharic, English and a little bit of Suomi(Finnish)
1729+
*/
16041730
```
1731+
### Spread or Rest Operator
1732+
1733+
#### Exercise: Destructuring
1734+
```js
1735+
const constants = [2.72, 3.14, 9.81,37, 100]
1736+
const countries = ['Finland', 'Estonia', 'Sweden', 'Denmark', 'Norway']
1737+
1738+
const rectangle = {
1739+
width:20,
1740+
height:10,
1741+
area:200,
1742+
perimeter:60
1743+
}
16051744

1606-
=== not completed ==
1745+
```
1746+
1. Assign the elements of constants array to e, pi, gravity, humanBodyTemp, waterBoilingTemp.
1747+
2. Assign the elements of countries array to fin, est, sw, den, nor
1748+
3. Destructure the rectangle object by its propertis or keys.
16071749
16081750
## Document Object Model
16091751
HTML document is structured as a JavaScript Object. Every HTML element has a different properties which can help to manipulate it. It is possible to get, create, append or remove HTML elements using JavaScript. Check the examples below. Selecting HTML element using JavaScript is similar to select CSS. To select an HTML element, we use tag name, id, class name. To create an HTML element we use tag name.
@@ -1927,7 +2069,7 @@ localStorage.clear();
19272069
- [Conditionals](#conditionals)
19282070
- [If](#if)
19292071
- [If Else](#if-else)
1930-
- [If Else if else](#if-else-if-else)
2072+
- [If else if else](#if-else-if-else)
19312073
- [Switch](#switch)
19322074
- [Ternary Operators](#ternary-operators)
19332075
- [Exercise - 8 : Conditionals](#exercise---8--conditionals)
@@ -1958,6 +2100,7 @@ localStorage.clear();
19582100
- [Destructing Arrays](#destructing-arrays)
19592101
- [Destructuring Object](#destructuring-object)
19602102
- [Renaming during structuring](#renaming-during-structuring)
2103+
- [Spread or Rest Operator](#spread-or-rest-operator)
19612104
- [Document Object Model](#document-object-model)
19622105
- [Getting Element](#getting-element)
19632106
- [Getting elements by tag name](#getting-elements-by-tag-name)

0 commit comments

Comments
 (0)