fixed instructions for exercise level 3 #8

pull/846/head
Henry Hart Alegrado 2 years ago
parent 2e51344ea3
commit ee308b0a98

@ -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)
@ -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,11 +139,11 @@ 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
``` ```
@ -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 = [
'HTML',
'CSS',
'JavaScript',
'React',
'Redux',
'Node',
'MongoDB'
]
const mernStack = ['MongoDB', 'Express', 'React', 'Node'] const webTechs = [
``` "HTML",
"CSS",
"JavaScript",
"React",
"Redux",
"Node",
"MongoDB",
];
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
@ -414,48 +411,48 @@ for(let i = 0; i <= 5; i++){
] ]
``` ```
2. 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'. 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 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'. 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 countries array above, 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 countries array above, find the country or countries 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
@ -476,7 +473,7 @@ for(let i = 0; i <= 5; i++){
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