day 9 output format changed

pull/103/head
Asabeneh 4 years ago
parent 585c33e790
commit 41a3f95b1a

@ -11,6 +11,7 @@
<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 8](../08_Day_Objects/08_day_objects.md) | [Day 10 >>](../10_Day_Sets_and_Maps/10_day_Sets_and_Maps.md) [<< Day 8](../08_Day_Objects/08_day_objects.md) | [Day 10 >>](../10_Day_Sets_and_Maps/10_day_Sets_and_Maps.md)
@ -222,7 +223,7 @@ console.log(sum)
```js ```js
const countries = ['Finland', 'Denmark', 'Sweden', 'Norway', 'Iceland'] const countries = ['Finland', 'Denmark', 'Sweden', 'Norway', 'Iceland']
countries.forEach(element => console.log(element.toUpperCase())) countries.forEach((element) => console.log(element.toUpperCase()))
``` ```
```sh ```sh
@ -249,7 +250,7 @@ const modifiedArray = arr.map((element,index) => element);
*/ */
//Example //Example
const numbers = [1, 2, 3, 4, 5] const numbers = [1, 2, 3, 4, 5]
const numbersSquare = numbers.map(num => num * num) const numbersSquare = numbers.map((num) => num * num)
console.log(numbersSquare) console.log(numbersSquare)
``` ```
@ -260,7 +261,7 @@ console.log(numbersSquare)
```js ```js
const names = ['Asabeneh', 'Mathias', 'Elias', 'Brook'] const names = ['Asabeneh', 'Mathias', 'Elias', 'Brook']
const namesToUpperCase = names.map(name => name.toUpperCase()) const namesToUpperCase = names.map((name) => name.toUpperCase())
console.log(namesToUpperCase) console.log(namesToUpperCase)
``` ```
@ -280,9 +281,9 @@ const countries = [
'Hungary', 'Hungary',
'Ireland', 'Ireland',
'Japan', 'Japan',
'Kenya' 'Kenya',
] ]
const countriesToUpperCase = countries.map(country => country.toUpperCase()) const countriesToUpperCase = countries.map((country) => country.toUpperCase())
console.log(countriesToUpperCase) console.log(countriesToUpperCase)
/* /*
@ -300,7 +301,7 @@ const countriesToUpperCase = countries.map(country => country.toUpperCase());
``` ```
```js ```js
const countriesFirstThreeLetters = countries.map(country => const countriesFirstThreeLetters = countries.map((country) =>
country.toUpperCase().slice(0, 3) country.toUpperCase().slice(0, 3)
) )
``` ```
@ -315,7 +316,7 @@ _Filter_: Filter out items which full fill filtering conditions and return a new
```js ```js
//Filter countries containing land //Filter countries containing land
const countriesContainingLand = countries.filter(country => const countriesContainingLand = countries.filter((country) =>
country.includes('land') country.includes('land')
) )
console.log(countriesContainingLand) console.log(countriesContainingLand)
@ -326,7 +327,7 @@ console.log(countriesContainingLand)
``` ```
```js ```js
const countriesEndsByia = countries.filter(country => country.includes('ia')) const countriesEndsByia = countries.filter((country) => country.includes('ia'))
console.log(countriesEndsByia) console.log(countriesEndsByia)
``` ```
@ -336,7 +337,7 @@ console.log(countriesEndsByia)
```js ```js
const countriesHaveFiveLetters = countries.filter( const countriesHaveFiveLetters = countries.filter(
country => country.length === 5 (country) => country.length === 5
) )
console.log(countriesHaveFiveLetters) console.log(countriesHaveFiveLetters)
``` ```
@ -351,10 +352,10 @@ const scores = [
{ name: 'Mathias', score: 80 }, { name: 'Mathias', score: 80 },
{ name: 'Elias', score: 50 }, { name: 'Elias', score: 50 },
{ name: 'Martha', score: 85 }, { name: 'Martha', score: 85 },
{ name: 'John', score: 100 } { name: 'John', score: 100 },
] ]
const scoresGreaterEight = scores.filter(score => score.score > 80) const scoresGreaterEight = scores.filter((score) => score.score > 80)
console.log(scoresGreaterEight) console.log(scoresGreaterEight)
``` ```
@ -383,7 +384,7 @@ _every_: Check if all the elements are similar in one aspect. It returns boolean
```js ```js
const names = ['Asabeneh', 'Mathias', 'Elias', 'Brook'] const names = ['Asabeneh', 'Mathias', 'Elias', 'Brook']
const areAllStr = names.every(name => typeof name === 'string') const areAllStr = names.every((name) => typeof name === 'string')
console.log(arrAllStr) console.log(arrAllStr)
``` ```
@ -394,7 +395,7 @@ true
```js ```js
const bools = [true, true, true, true] const bools = [true, true, true, true]
const areAllTrue = bools.every(b => { const areAllTrue = bools.every((b) => {
return b === true return b === true
}) })
@ -412,7 +413,7 @@ _find_: Return the first element which satisfies the condition
```js ```js
const ages = [24, 22, 25, 32, 35, 18] const ages = [24, 22, 25, 32, 35, 18]
const age = ages.find(age => age < 20) const age = ages.find((age) => age < 20)
console.log(age) console.log(age)
``` ```
@ -423,7 +424,7 @@ console.log(age)
```js ```js
const names = ['Asabeneh', 'Mathias', 'Elias', 'Brook'] const names = ['Asabeneh', 'Mathias', 'Elias', 'Brook']
const result = names.find(name => name.length > 7) const result = names.find((name) => name.length > 7)
console.log(result) console.log(result)
``` ```
@ -437,10 +438,10 @@ const scores = [
{ name: 'Mathias', score: 80 }, { name: 'Mathias', score: 80 },
{ name: 'Elias', score: 50 }, { name: 'Elias', score: 50 },
{ name: 'Martha', score: 85 }, { name: 'Martha', score: 85 },
{ name: 'John', score: 100 } { name: 'John', score: 100 },
] ]
const score = scores.find(user => { const score = scores.find((user) => {
return user.score > 80 return user.score > 80
}) })
console.log(score) console.log(score)
@ -458,10 +459,10 @@ _findIndex_: Return the position of the first element which satisfies the condit
const names = ['Asabeneh', 'Mathias', 'Elias', 'Brook'] const names = ['Asabeneh', 'Mathias', 'Elias', 'Brook']
const ages = [24, 22, 25, 32, 35, 18] const ages = [24, 22, 25, 32, 35, 18]
const result = names.findIndex(name => name.length > 7) const result = names.findIndex((name) => name.length > 7)
console.log(result) // 0 console.log(result) // 0
const age = ages.findIndex(age => age < 20) const age = ages.findIndex((age) => age < 20)
console.log(age) // 5 console.log(age) // 5
``` ```
@ -473,7 +474,7 @@ _some_: Check if some of the elements are similar in one aspect. It returns bool
const names = ['Asabeneh', 'Mathias', 'Elias', 'Brook'] const names = ['Asabeneh', 'Mathias', 'Elias', 'Brook']
const bools = [true, true, true, true] const bools = [true, true, true, true]
const areSomeTrue = bools.some(b => { const areSomeTrue = bools.some((b) => {
return b === true return b === true
}) })
@ -481,11 +482,10 @@ console.log(areSomeTrue) //true
``` ```
```js ```js
const areAllStr = names.some(name => typeof name === 'number') const areAllStr = names.some((name) => typeof name === 'number')
console.log(areAllStr) // false console.log(areAllStr) // false
``` ```
### sort ### sort
_sort_: The sort methods arranges the array elements either ascending or descending order. By default, the **_sort()_** method sorts values as strings.This works well for string array items but not for numbers. If number values are sorted as strings and it give us wrong result. Sort method modify the original array. It is recommended to copy the original data before you start using _sort_ method. _sort_: The sort methods arranges the array elements either ascending or descending order. By default, the **_sort()_** method sorts values as strings.This works well for string array items but not for numbers. If number values are sorted as strings and it give us wrong result. Sort method modify the original array. It is recommended to copy the original data before you start using _sort_ method.
@ -541,7 +541,7 @@ const users = [
{ name: 'Asabeneh', age: 150 }, { name: 'Asabeneh', age: 150 },
{ name: 'Brook', age: 50 }, { name: 'Brook', age: 50 },
{ name: 'Eyo', age: 100 }, { name: 'Eyo', age: 100 },
{ name: 'Elias', age: 22 } { name: 'Elias', age: 22 },
] ]
users.sort((a, b) => { users.sort((a, b) => {
if (a.age < b.age) return -1 if (a.age < b.age) return -1
@ -568,7 +568,7 @@ const products = [
{ product: 'potato', price: ' ' }, { product: 'potato', price: ' ' },
{ product: 'avocado', price: 8 }, { product: 'avocado', price: 8 },
{ product: 'coffee', price: 10 }, { product: 'coffee', price: 10 },
{ product: 'tea', price: '' } { product: 'tea', price: '' },
] ]
``` ```
@ -587,17 +587,17 @@ const products = [
13. Use **_filter_** to filter out countries containing six letters and more in the country array. 13. Use **_filter_** to filter out countries containing six letters and more in the country array.
14. Use **_filter_** to filter out country start with 'E'; 14. Use **_filter_** to filter out country start with 'E';
15. Use **_filter_** to filter out only prices with values. 15. Use **_filter_** to filter out only prices with values.
18. Declare a function called getStringLists which takes an array as a parameter and then returns an array only with string items. 16. Declare a function called getStringLists which takes an array as a parameter and then returns an array only with string items.
19. Use **_reduce_** to sum all the numbers in the numbers array. 17. Use **_reduce_** to sum all the numbers in the numbers array.
20. Use **_reduce_** to concatenate all the countries and to produce this sentence: **_Estonia, Finland, Sweden, Denmark, Norway, and IceLand are north European countries_** 18. Use **_reduce_** to concatenate all the countries and to produce this sentence: **_Estonia, Finland, Sweden, Denmark, Norway, and IceLand are north European countries_**
21. Explain the difference between **_some_** and **_every_** 19. Explain the difference between **_some_** and **_every_**
22. Use **_some_** to check if some names' length greater than seven in names array 20. Use **_some_** to check if some names' length greater than seven in names array
23. Use **_every_** to check if all the countries contain the word land 21. Use **_every_** to check if all the countries contain the word land
24. Explain the difference between **_find_** and **_findIndex_**. 22. Explain the difference between **_find_** and **_findIndex_**.
25. Use **_find_** to find the first country containing only six letters in the countries array 23. Use **_find_** to find the first country containing only six letters in the countries array
26. Use **_findIndex_** to find the position of the first country containing only six letters in the countries array 24. Use **_findIndex_** to find the position of the first country containing only six letters in the countries array
27. Use **_findIndex_** to find the position of **_Norway_** if it doesn't exist in the array you will get -1. 25. Use **_findIndex_** to find the position of **_Norway_** if it doesn't exist in the array you will get -1.
28. Use **_findIndex_** to find the position of **_Russia_** if it doesn't exist in the array you will get -1. 26. Use **_findIndex_** to find the position of **_Russia_** if it doesn't exist in the array you will get -1.
### Exercises: Level 2 ### Exercises: Level 2
@ -617,24 +617,26 @@ const products = [
````js ````js
// Your output should look like this // Your output should look like this
console.log(mostSpokenLanguages(countries, 10)) console.log(mostSpokenLanguages(countries, 10))
[(91, 'English'), [
(45, 'French'), {country: 'English',count:91},
(25, 'Arabic'), {country: 'French',count:45},
(24, 'Spanish'), {country: 'Arabic',count:25},
(9, 'Russian'), {country: 'Spanish',count:24},
(9, 'Portuguese'), {country:'Russian',count:9},
(8, 'Dutch'), {country:'Portuguese', count:9},
(7, 'German'), {country:'Dutch',count:8},
(5, 'Chinese'), {country:'German',count:7},
(4, 'Swahili'), {country:'Chinese',count:5},
(4, 'Serbian')] {country:'Swahili',count:4},
{country:'Serbian',count:4}
]
// Your output should look like this // Your output should look like this
console.log(mostSpokenLanguages(countries, 3)) console.log(mostSpokenLanguages(countries, 3))
[ [
(91, 'English'), {country: 'English',count: 91},
(45, 'French'), {country: 'French',count: 45},
(25, 'Arabic') {country: 'Arabic',count: 25},
]``` ]```
```` ````
@ -665,6 +667,8 @@ const products = [
] ]
``` ```
````
1. \*\*\* Try to develop a program which calculate measure of central tendency of a sample(mean, median, mode) and measure of variability(range, variance, standard deviation). In addition to those measures find the min, max, count, percentile, and frequency distribution of the sample. You can create an object called statistics and create all the functions which do statistical calculations as method for the statistics object. Check the output below. 1. \*\*\* Try to develop a program which calculate measure of central tendency of a sample(mean, median, mode) and measure of variability(range, variance, standard deviation). In addition to those measures find the min, max, count, percentile, and frequency distribution of the sample. You can create an object called statistics and create all the functions which do statistical calculations as method for the statistics object. Check the output below.
```js ```js

Loading…
Cancel
Save