You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
3.1 KiB
3.1 KiB
Day 10 - Set And Maps
Exercise:Solutions
Home | << Day 9 | Day 11 >>
Exercise Solutions
Exercises Level 1
- create an empty set
// app.js
const countries = new Set()
console.log(countries)
- Create a set containing 0 to 10 using loop
// app.js
const set = new Set()
set.add(0,)
set.add(1)
set.add(2)
set.add(3)
set.add(4)
set.add(5)
set.add(6)
set.add(7)
set.add(8)
set.add(9)
set.add(10)
console.log(set)
- Remove an element from a set
// app.js
const set = new Set()
set.add(0,)
set.add(1)
set.add(2)
console.log(set.delete(1))
- Clear a set
// app.js
const set = new Set()
set.add(0,)
set.add(1)
set.add(2)
set.clear()
console.log(set)
- Create a set of 5 string elements from array
// app.js
const arr = ['ayşe','fatma','dilan','cansu']
const set = new Set(arr)
console.log(set)
- Create a map of countries and number of characters of a country
// app.js
const map = new Map()
map.set(1,'turkey')
map.set(2,'russıa')
map.set(3,'germaby')
console.log(map)
Exercises Level 2
- Find a union b
//app.js
const a = [4, 5, 8, 9]
const b = [3, 4, 5, 7]
const c =[ ...a,...b
]
const A = new Set(a)
const B = new Set(b)
const C = new Set(c)
console.log(C)
- Find a intersection b
//app.js
const a = [4, 5, 8, 9]
const b = [3, 4, 5, 7]
const A = new Set(a)
const B = new Set(b)
const aUnionB =a.filter((num) => B.has(num))
console.log(aUnionB)
- Find a with b
//app.js
const a = [4, 5, 8, 9]
const b = [3, 4, 5, 7]
const A = new Set(a)
const B = new Set(b)
const aUnionB =a.filter((num) => !B.has(num))
console.log(aUnionB)
Exercises Level 3
-
How many languages are there in the countries object file.
-
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}
]
// app.js
// 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}
]