From f270fd65af2001d68e242f6ddfe78ca5dad08920 Mon Sep 17 00:00:00 2001 From: Jose Linardo Date: Sun, 26 Jun 2022 15:38:00 -0500 Subject: [PATCH 1/2] day 10 level 1 done --- .../10_day_starter/scripts/main.js | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/10_Day_Sets_and_Maps/10_day_starter/scripts/main.js b/10_Day_Sets_and_Maps/10_day_starter/scripts/main.js index c6045c8..5f5eff7 100644 --- a/10_Day_Sets_and_Maps/10_day_starter/scripts/main.js +++ b/10_Day_Sets_and_Maps/10_day_starter/scripts/main.js @@ -1,2 +1,43 @@ console.log(countries) -alert('Open the console and check if the countries has been loaded') \ No newline at end of file +//alert('Open the console and check if the countries has been loaded') + +//Exercises:Level 1 +/*const a = [4, 5, 8, 9] +const b = [3, 4, 5, 7] +const countries = ['Finland', 'Sweden', 'Norway']*/ + +//1.create an empty set +const set = new Set(); +//2.Create a set containing 0 to 10 using loop +const set1 = new Set(); +for (let i = 0; i < 10; i++) { + set.add(i); +} + +console.log(set); +//3. Remove an element from a set +const arr = [2, 8, 10, 7]; +const set2 = new Set(arr); +set.delete(10); + +console.log(set2); // Set(3) {2, 8, 7} +//4. Clear a set +const arr1 = ['a', 'b', 'c', 'd']; +const set3 = new Set(arr); +set.clear(); + +console.log(set3); +//5. Create a set of 5 string elements from array +const fruits = ['apple', 'mango', 'kiwi', 'peach', 'orange'] +const setOfFruits = new Set(fruits); + +console.log(setOfFruits); +//6. Create a map of countries and number of characters of a country +const countries = ['Finland', 'Sweden', 'Denmark', 'Norway', 'IceLand']; +const mapOfCountries = new Map(); + +for (const ctr of countries) { + mapOfCountries.set(ctr, ctr.length) +} + +console.log(mapOfCountries); From d76074f097e1be9e7654152b350c11de6f90884b Mon Sep 17 00:00:00 2001 From: Jose Linardo Date: Sun, 26 Jun 2022 21:00:03 -0500 Subject: [PATCH 2/2] day 10 level 3 done --- .../10_day_starter/scripts/main.js | 93 +++++++++++++++++-- 1 file changed, 84 insertions(+), 9 deletions(-) diff --git a/10_Day_Sets_and_Maps/10_day_starter/scripts/main.js b/10_Day_Sets_and_Maps/10_day_starter/scripts/main.js index 5f5eff7..00fb961 100644 --- a/10_Day_Sets_and_Maps/10_day_starter/scripts/main.js +++ b/10_Day_Sets_and_Maps/10_day_starter/scripts/main.js @@ -11,20 +11,19 @@ const set = new Set(); //2.Create a set containing 0 to 10 using loop const set1 = new Set(); for (let i = 0; i < 10; i++) { - set.add(i); + set1.add(i); //Set(10) {0, 1, 2, 3, 4, …} } - -console.log(set); +console.log(set1); //3. Remove an element from a set const arr = [2, 8, 10, 7]; const set2 = new Set(arr); -set.delete(10); +set2.delete(10); //Set(3) {2, 8, 7} console.log(set2); // Set(3) {2, 8, 7} //4. Clear a set const arr1 = ['a', 'b', 'c', 'd']; const set3 = new Set(arr); -set.clear(); +set3.clear(); //Set(0) {size: 0} console.log(set3); //5. Create a set of 5 string elements from array @@ -33,11 +32,87 @@ const setOfFruits = new Set(fruits); console.log(setOfFruits); //6. Create a map of countries and number of characters of a country -const countries = ['Finland', 'Sweden', 'Denmark', 'Norway', 'IceLand']; +const countries_ = ['Finland', 'Sweden', 'Denmark', 'Norway', 'IceLand']; const mapOfCountries = new Map(); -for (const ctr of countries) { - mapOfCountries.set(ctr, ctr.length) +for (const country of countries_) { + mapOfCountries.set(country, country.length) +} +console.log(mapOfCountries) + +//Exercises:Level 2 + +//1. Find a union b +let a = [17, 54, 23, 98, 19] +let b = [18, 22, 17, 20, 21, 98] +let c = [...a, ...b] + +let C = new Set(c); + +console.log(C); +//2. Find a intersection b +let A = new Set(a); +let B = new Set(b) + +let d = a.filter(num => B.has(num)) +let D = new Set(d) + +console.log(D) +//3. Find a with b +let e = a.filter(num => !B.has(num)) +let E = new Set(e) +console.log(E) + +// Exercises:Level 3 + +//1. How many languages are there in the countries object file. +const languages = countries.map((element) => element.languages); +console.log(languages) +const unionLangs = languages.reduce((acc, cur) => [...acc, ...cur], []) +const uniqLangs = new Set(unionLangs) +console.log(uniqLangs.size) //112 + +//2. Use the countries data to find the 10 most spoken languages: +// Your output should look like this +/*console.log(mostSpokenLanguages(countries, 10)) +[ + { English: 91 }, + { French: 45 }, + { Arabic: 25 }, + { Spanish: 24 }, + { Russian: 9 }, + { Portuguese: 9 }, + { Dutch: 8 }, + { German: 7 }, + { Chinese: 5 }, + { Swahili: 4 }, + { Serbian: 4 } +] + +// Your output should look like this +console.log(mostSpokenLanguages(countries, 3)) +[ +{English:91}, +{French:45}, +{Arabic:25} +]*/ + +const langArr = []; +for (const l of uniqLangs) { + const count = unionLangs.filter((lang) => lang === l).length; + langArr.push({ [l] : count}); +} + +const sortedLang = langArr.sort((a, b) => { + return Object.values(b) - Object.values(a) +}); + +console.log(sortedLang) + +function mostSpokenLanguages(num) { + const langObject = sortedLang.filter((element) => + sortedLang.indexOf(element) < num); + return langObject; } +console.log(mostSpokenLanguages(10)); -console.log(mapOfCountries);