pull/846/merge
Henry Hart Alegrado 3 years ago committed by GitHub
commit 81e0574a44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -7,10 +7,11 @@
<img alt="Twitter Follow" src="https://img.shields.io/twitter/follow/asabeneh?style=social"> <img alt="Twitter Follow" src="https://img.shields.io/twitter/follow/asabeneh?style=social">
</a> </a>
<sub>Author: <sub>Author:
<a href="https://www.linkedin.com/in/asabeneh/" target="_blank">Asabeneh Yetayeh</a><br> <a href="https://www.linkedin.com/in/asabeneh/" target="_blank">Asabeneh Yetayeh</a><br>
<small> January, 2020</small> <small> January, 2020</small>
</sub> </sub>
</div> </div>
[<< Day 5](../05_Day_Arrays/05_day_arrays.md) | [Day 7 >>](../07_Day_Functions/07_day_functions.md) [<< Day 5](../05_Day_Arrays/05_day_arrays.md) | [Day 7 >>](../07_Day_Functions/07_day_functions.md)
@ -18,17 +19,17 @@
![Day 5](../images/banners/day_1_6.png) ![Day 5](../images/banners/day_1_6.png)
- [📔 Day 6](#-day-6) - [📔 Day 6](#-day-6)
- [Loops](#loops) - [Loops](#loops)
- [for Loop](#for-loop) - [for Loop](#for-loop)
- [while loop](#while-loop) - [while loop](#while-loop)
- [do while loop](#do-while-loop) - [do while loop](#do-while-loop)
- [for of loop](#for-of-loop) - [for of loop](#for-of-loop)
- [break](#break) - [break](#break)
- [continue](#continue) - [continue](#continue)
- [💻 Exercises:Day 6](#-exercisesday-6) - [💻 Exercises:Day 6](#-exercisesday-6)
- [Exercises: Level 1](#exercises-level-1) - [Exercises: Level 1](#exercises-level-1)
- [Exercises: Level 2](#exercises-level-2) - [Exercises: Level 2](#exercises-level-2)
- [Exercises: Level 3](#exercises-level-3) - [Exercises: Level 3](#exercises-level-3)
# 📔 Day 6 # 📔 Day 6
@ -48,24 +49,24 @@ for(initialization, condition, increment/decrement){
``` ```
```js ```js
for(let i = 0; i <= 5; i++){ for (let i = 0; i <= 5; i++) {
console.log(i) console.log(i);
} }
// 0 1 2 3 4 5 // 0 1 2 3 4 5
``` ```
```js ```js
for(let i = 5; i >= 0; i--){ for (let i = 5; i >= 0; i--) {
console.log(i) console.log(i);
} }
// 5 4 3 2 1 0 // 5 4 3 2 1 0
``` ```
```js ```js
for(let i = 0; i <= 5; i++){ for (let i = 0; i <= 5; i++) {
console.log(`${i} * ${i} = ${i * i}`) console.log(`${i} * ${i} = ${i * i}`);
} }
``` ```
@ -79,10 +80,10 @@ for(let i = 0; i <= 5; i++){
``` ```
```js ```js
const countries = ['Finland', 'Sweden', 'Denmark', 'Norway', 'Iceland'] const countries = ["Finland", "Sweden", "Denmark", "Norway", "Iceland"];
const newArr = [] const newArr = [];
for(let i = 0; i < countries.length; i++){ for (let i = 0; i < countries.length; i++) {
newArr.push(countries[i].toUpperCase()) newArr.push(countries[i].toUpperCase());
} }
// ["FINLAND", "SWEDEN", "DENMARK", "NORWAY", "ICELAND"] // ["FINLAND", "SWEDEN", "DENMARK", "NORWAY", "ICELAND"]
@ -91,47 +92,45 @@ for(let i = 0; i < countries.length; i++){
Adding all elements in the array Adding all elements in the array
```js ```js
const numbers = [1, 2, 3, 4, 5] const numbers = [1, 2, 3, 4, 5];
let sum = 0 let sum = 0;
for(let i = 0; i < numbers.length; i++){ for (let i = 0; i < numbers.length; i++) {
sum = sum + numbers[i] // can be shorten, sum += numbers[i] sum = sum + numbers[i]; // can be shorten, sum += numbers[i]
} }
console.log(sum) // 15 console.log(sum); // 15
``` ```
Creating a new array based on the existing array Creating a new array based on the existing array
```js ```js
const numbers = [1, 2, 3, 4, 5] const numbers = [1, 2, 3, 4, 5];
const newArr = [] const newArr = [];
let sum = 0 let sum = 0;
for(let i = 0; i < numbers.length; i++){ for (let i = 0; i < numbers.length; i++) {
newArr.push( numbers[i] ** 2) newArr.push(numbers[i] ** 2);
} }
console.log(newArr) // [1, 4, 9, 16, 25] console.log(newArr); // [1, 4, 9, 16, 25]
``` ```
```js ```js
const countries = ['Finland', 'Sweden', 'Norway', 'Denmark', 'Iceland'] const countries = ["Finland", "Sweden", "Norway", "Denmark", "Iceland"];
const newArr = [] const newArr = [];
for(let i = 0; i < countries.length; i++){ for (let i = 0; i < countries.length; i++) {
newArr.push(countries[i].toUpperCase()) newArr.push(countries[i].toUpperCase());
} }
console.log(newArr) // ["FINLAND", "SWEDEN", "NORWAY", "DENMARK", "ICELAND"] console.log(newArr); // ["FINLAND", "SWEDEN", "NORWAY", "DENMARK", "ICELAND"]
``` ```
### while loop ### while loop
```js ```js
let i = 0 let i = 0;
while (i <= 5) { while (i <= 5) {
console.log(i) console.log(i);
i++ i++;
} }
// 0 1 2 3 4 5 // 0 1 2 3 4 5
@ -140,18 +139,18 @@ while (i <= 5) {
### do while loop ### do while loop
```js ```js
let i = 0 let i = 0;
do { do {
console.log(i) console.log(i);
i++ i++;
} while (i <= 5) } while (i <= 5);
// 0 1 2 3 4 5 // 0 1 2 3 4 5
``` ```
### for of loop ### for of loop
We use for of loop for arrays. It is very hand way to iterate through an array if we are not interested in the index of each element in the array. We use for of loop for arrays. It is a very handy way to iterate through an array if we are not interested in the index of each element in the array.
```js ```js
for (const element of arr) { for (const element of arr) {
@ -160,60 +159,58 @@ for (const element of arr) {
``` ```
```js ```js
const numbers = [1, 2, 3, 4, 5];
const numbers = [1, 2, 3, 4, 5]
for (const num of numbers) { for (const num of numbers) {
console.log(num) console.log(num);
} }
// 1 2 3 4 5 // 1 2 3 4 5
for (const num of numbers) { for (const num of numbers) {
console.log(num * num) console.log(num * num);
} }
// 1 4 9 16 25 // 1 4 9 16 25
// adding all the numbers in the array // adding all the numbers in the array
let sum = 0 let sum = 0;
for (const num of numbers) { for (const num of numbers) {
sum = sum + num sum = sum + num;
// can be also shorten like this, sum += num // can be also shorten like this, sum += num
// after this we will use the shorter synthax(+=, -=, *=, /= etc) // after this we will use the shorter synthax(+=, -=, *=, /= etc)
} }
console.log(sum) // 15 console.log(sum); // 15
const webTechs = [ const webTechs = [
'HTML', "HTML",
'CSS', "CSS",
'JavaScript', "JavaScript",
'React', "React",
'Redux', "Redux",
'Node', "Node",
'MongoDB' "MongoDB",
] ];
for (const tech of webTechs) { for (const tech of webTechs) {
console.log(tech.toUpperCase()) console.log(tech.toUpperCase());
} }
// HTML CSS JAVASCRIPT REACT NODE MONGODB // HTML CSS JAVASCRIPT REACT NODE MONGODB
for (const tech of webTechs) { for (const tech of webTechs) {
console.log(tech[0]) // get only the first letter of each element, H C J R N M console.log(tech[0]); // get only the first letter of each element, H C J R N M
} }
``` ```
```js ```js
const countries = ['Finland', 'Sweden', 'Norway', 'Denmark', 'Iceland'] const countries = ["Finland", "Sweden", "Norway", "Denmark", "Iceland"];
const newArr = [] const newArr = [];
for(const country of countries){ for (const country of countries) {
newArr.push(country.toUpperCase()) newArr.push(country.toUpperCase());
} }
console.log(newArr) // ["FINLAND", "SWEDEN", "NORWAY", "DENMARK", "ICELAND"] console.log(newArr); // ["FINLAND", "SWEDEN", "NORWAY", "DENMARK", "ICELAND"]
``` ```
### break ### break
@ -221,11 +218,11 @@ console.log(newArr) // ["FINLAND", "SWEDEN", "NORWAY", "DENMARK", "ICELAND"]
Break is used to interrupt a loop. Break is used to interrupt a loop.
```js ```js
for(let i = 0; i <= 5; i++){ for (let i = 0; i <= 5; i++) {
if(i == 3){ if (i == 3) {
break break;
} }
console.log(i) console.log(i);
} }
// 0 1 2 // 0 1 2
@ -235,14 +232,14 @@ The above code stops if 3 found in the iteration process.
### continue ### continue
We use the keyword *continue* to skip a certain iterations. We use the keyword _continue_ to skip a certain iterations.
```js ```js
for(let i = 0; i <= 5; i++){ for (let i = 0; i <= 5; i++) {
if(i == 3){ if (i == 3) {
continue continue;
} }
console.log(i) console.log(i);
} }
// 0 1 2 4 5 // 0 1 2 4 5
@ -254,33 +251,33 @@ for(let i = 0; i <= 5; i++){
### Exercises: Level 1 ### Exercises: Level 1
```js ```js
const countries = [ const countries = [
'Albania', "Albania",
'Bolivia', "Bolivia",
'Canada', "Canada",
'Denmark', "Denmark",
'Ethiopia', "Ethiopia",
'Finland', "Finland",
'Germany', "Germany",
'Hungary', "Hungary",
'Ireland', "Ireland",
'Japan', "Japan",
'Kenya' "Kenya",
] ];
const webTechs = [ const webTechs = [
'HTML', "HTML",
'CSS', "CSS",
'JavaScript', "JavaScript",
'React', "React",
'Redux', "Redux",
'Node', "Node",
'MongoDB' "MongoDB",
] ];
const mernStack = ['MongoDB', 'Express', 'React', 'Node'] const mernStack = ["MongoDB", "Express", "React", "Node"];
``` ```
1. Iterate 0 to 10 using for loop, do the same using while and do while loop 1. Iterate 0 to 10 using for loop, do the same using while and do while loop
2. Iterate 10 to 0 using for loop, do the same using while and do while loop 2. Iterate 10 to 0 using for loop, do the same using while and do while loop
@ -351,8 +348,8 @@ for(let i = 0; i <= 5; i++){
[2550, 2500] [2550, 2500]
``` ```
13. Develop a small script which generate array of 5 random numbers 13. Develop a small script which generate an array of 5 random numbers
14. Develop a small script which generate array of 5 random numbers and the numbers must be unique 14. Develop a small script which generate an array of 5 random numbers and the numbers must be unique
15. Develop a small script which generate a six characters random id: 15. Develop a small script which generate a six characters random id:
```sh ```sh
@ -363,121 +360,120 @@ for(let i = 0; i <= 5; i++){
1. Develop a small script which generate any number of characters random id: 1. Develop a small script which generate any number of characters random id:
```sh ```sh
fe3jo1gl124g fe3jo1gl124g
``` ```
```sh ```sh
xkqci4utda1lmbelpkm03rba xkqci4utda1lmbelpkm03rba
``` ```
1. Write a script which generates a random hexadecimal number. 1. Write a script which generates a random hexadecimal number.
```sh ```sh
'#ee33df' '#ee33df'
``` ```
1. Write a script which generates a random rgb color number. 1. Write a script which generates a random rgb color number.
```sh ```sh
rgb(240,180,80) rgb(240,180,80)
``` ```
1. Using the above countries array, create the following new array. 1. Using the above countries array, create the following new array.
```sh ```sh
["ALBANIA", "BOLIVIA", "CANADA", "DENMARK", "ETHIOPIA", "FINLAND", "GERMANY", "HUNGARY", "IRELAND", "JAPAN", "KENYA"] ["ALBANIA", "BOLIVIA", "CANADA", "DENMARK", "ETHIOPIA", "FINLAND", "GERMANY", "HUNGARY", "IRELAND", "JAPAN", "KENYA"]
``` ```
1. Using the above countries array, create an array for countries length'. 1. Using the above countries array, create an array for countries length'.
```sh ```sh
[7, 7, 6, 7, 8, 7, 7, 7, 7, 5, 5] [7, 7, 6, 7, 8, 7, 7, 7, 7, 5, 5]
``` ```
1. Use the countries array to create the following array of arrays: 1. Use the countries array to create the following array of arrays:
```sh ```sh
[ [
['Albania', 'ALB', 7], ['Albania', 'ALB', 7],
['Bolivia', 'BOL', 7], ['Bolivia', 'BOL', 7],
['Canada', 'CAN', 6], ['Canada', 'CAN', 6],
['Denmark', 'DEN', 7], ['Denmark', 'DEN', 7],
['Ethiopia', 'ETH', 8], ['Ethiopia', 'ETH', 8],
['Finland', 'FIN', 7], ['Finland', 'FIN', 7],
['Germany', 'GER', 7], ['Germany', 'GER', 7],
['Hungary', 'HUN', 7], ['Hungary', 'HUN', 7],
['Ireland', 'IRE', 7], ['Ireland', 'IRE', 7],
['Iceland', 'ICE', 7], ['Iceland', 'ICE', 7],
['Japan', 'JAP', 5], ['Japan', 'JAP', 5],
['Kenya', 'KEN', 5] ['Kenya', 'KEN', 5]
] ]
``` ```
2. In above countries array, check if there is a country or countries containing the word 'land'. If there are countries containing 'land', print it as array. If there is no country containing the word 'land', print 'All these countries are without land'. 1. In the countries array above, check if there is a country or countries containing the word 'land'. If there are countries containing 'land', print it as array. If there is no country containing the word 'land', print 'All these countries are without land'.
```sh ```sh
['Finland','Ireland', 'Iceland'] ['Finland','Ireland', 'Iceland']
``` ```
3. In above countries array, check if there is a country or countries end with a substring 'ia'. If there are countries end with, print it as array. If there is no country containing the word 'ai', print 'These are countries ends without ia'. 1. In the countries array above, check if there is a country or countries that ends with a substring 'ia'. If there are countries that ends with 'ia', print it as an array. If there is no country containing the word 'ia', print 'These are countries that ends without ia'.
```sh ```sh
['Albania', 'Bolivia','Ethiopia'] ['Albania', 'Bolivia','Ethiopia']
``` ```
4. Using the above countries array, find the country containing the biggest number of characters. 1. Using the countries array above, find the country containing the biggest number of characters.
```sh ```sh
Ethiopia Ethiopia
``` ```
5. Using the above countries array, find the country containing only 5 characters. 1. Using the countries array above, find the country or countries containing only 5 characters.
```sh ```sh
['Japan', 'Kenya'] ['Japan', 'Kenya']
``` ```
6. Find the longest word in the webTechs array 1. Find the longest word in the webTechs array
7. Use the webTechs array to create the following array of arrays: 1. Use the webTechs array to create the following array of arrays:
```sh ```sh
[["HTML", 4], ["CSS", 3],["JavaScript", 10],["React", 5],["Redux", 5],["Node", 4],["MongoDB", 7]] [["HTML", 4], ["CSS", 3],["JavaScript", 10],["React", 5],["Redux", 5],["Node", 4],["MongoDB", 7]]
``` ```
8. An application created using MongoDB, Express, React and Node is called a MERN stack app. Create the acronym MERN by using the array mernStack 1. An application created using MongoDB, Express, React and Node is called a MERN stack app. Create the acronym MERN by using the array mernStack
9. Iterate through the array, ["HTML", "CSS", "JS", "React", "Redux", "Node", "Express", "MongoDB"] using a for loop or for of loop and print out the items. 1. Iterate through the array, ["HTML", "CSS", "JS", "React", "Redux", "Node", "Express", "MongoDB"] using a for loop or for of loop and print out the items.
10. This is a fruit array , ['banana', 'orange', 'mango', 'lemon'] reverse the order using loop without using a reverse method. 1. This is a fruit array , ['banana', 'orange', 'mango', 'lemon'] reverse the order using loop without using a reverse method.
11. Print all the elements of array as shown below. 1. Print all the elements of array as shown below.
```js ```js
const fullStack = [ const fullStack = [
['HTML', 'CSS', 'JS', 'React'], ["HTML", "CSS", "JS", "React"],
['Node', 'Express', 'MongoDB'] ["Node", "Express", "MongoDB"],
] ];
```` ```
```sh ```sh
HTML HTML
CSS CSS
JS JS
REACT REACT
NODE NODE
EXPRESS EXPRESS
MONGODB MONGODB
``` ```
### Exercises: Level 3 ### Exercises: Level 3
1. Copy countries array(Avoid mutation) 1. Copy countries array(Avoid mutation)
1. Arrays are mutable. Create a copy of array which does not modify the original. Sort the copied array and store in a variable sortedCountries 1. Arrays are mutable. Create a copy of array which does not modify the original. Sort the copied array and store in a variable sortedCountries
1. Sort the webTechs array and mernStack array 1. Sort the webTechs array and mernStack array
1. Extract all the countries contain the word 'land' from the [countries array](https://github.com/Asabeneh/30DaysOfJavaScript/tree/master/data/countries.js) and print it as array
1. Find the country containing the hightest number of characters in the [countries array](https://github.com/Asabeneh/30DaysOfJavaScript/tree/master/data/countries.js) 1. Find the country containing the hightest number of characters in the [countries array](https://github.com/Asabeneh/30DaysOfJavaScript/tree/master/data/countries.js)
1. Extract all the countries contain the word 'land' from the [countries array](https://github.com/Asabeneh/30DaysOfJavaScript/tree/master/data/countries.js) and print it as array 1. Extract all the countries contain the word 'land' from the [countries array](https://github.com/Asabeneh/30DaysOfJavaScript/tree/master/data/countries.js) and print it as array
1. Extract all the countries containing only four characters from the [countries array](https://github.com/Asabeneh/30DaysOfJavaScript/tree/master/data/countries.js) and print it as array 1. Extract all the countries containing only four characters from the [countries array](https://github.com/Asabeneh/30DaysOfJavaScript/tree/master/data/countries.js) and print it as array
1. Extract all the countries containing two or more words from the [countries array](https://github.com/Asabeneh/30DaysOfJavaScript/tree/master/data/countries.js) and print it as array 1. Extract all the countries containing two or more words from the [countries array](https://github.com/Asabeneh/30DaysOfJavaScript/tree/master/data/countries.js) and print it as array
1. Reverse the [countries array](https://github.com/Asabeneh/30DaysOfJavaScript/tree/master/data/countries.js) and capitalize each country and stored it as an array 1. Reverse the [countries array](https://github.com/Asabeneh/30DaysOfJavaScript/tree/master/data/countries.js) and capitalize each country then store it as an array
🎉 CONGRATULATIONS ! 🎉 🎉 CONGRATULATIONS ! 🎉

Loading…
Cancel
Save