-
Notifications
You must be signed in to change notification settings - Fork 0
/
maps_and_sets.js
88 lines (75 loc) · 2.37 KB
/
maps_and_sets.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Maps and Sets Exercise
// Quick Question #1
// What does the following code return?
// new Set([1,1,2,2,3,4])
// Answer - { 1, 2, 3, 4 }
// Quick Question #2
// What does the following code return?
// [...new Set("referee")].join("")
// Answer - "ref"
// Quick Questions #3
// What does the Map m look like after running the following code?
// let m = new Map();
// m.set([1,2,3], true);
// m.set([1,2,3], false);
// Answer - a map with the 'size' of 2,
// (
// [1,2,3] => true
// [1,2,3] => false
// )
// hasDuplicate
// Write a function called hasDuplicate which accepts an array and returns true or false if that array contains a duplicate
// hasDuplicate([1,3,2,1]) // true
// hasDuplicate([1,5,-1,4]) // false
// const hasDuplicate = (arr) => {
// let removeDupes = new Set(arr)
// if (removeDupes.size === arr.length) {
// return false
// }
// return true
// }
//console.log(hasDuplicate([1, 3, 2, 1])) // true
//console.log(hasDuplicate([1, 5, -1, 4])) // false
//
// vowelCount
// Write a function called vowelCount which accepts a string and returns a map where the keys are numbers and the values are the count of the vowels in the string.
// vowelCount('awesome') // Map { 'a' => 1, 'e' => 2, 'o' => 1 }
// vowelCount('Colt') // Map { 'o' => 1 }
// const vowelCount = (str) => {
// let aCount = 0;
// let eCount = 0;
// let iCount = 0;
// let oCount = 0;
// let uCount = 0;
// let spreadString = [...str]
// for (let letter of spreadString) {
// switch (letter) {
// case "a":
// aCount++
// break;
// case "e":
// eCount++
// break;
// case "i":
// iCount++
// break;
// case "o":
// oCount++
// break;
// case "u":
// uCount++
// break;
// default:
// break;
// }
// }
// let myMap = new Map()
// myMap.set('a', aCount)
// myMap.set('e', eCount)
// myMap.set('i', iCount)
// myMap.set('o', oCount)
// myMap.set('u', uCount)
// return myMap
// }
//console.log(vowelCount('awesome')) // Map { 'a' => 1, 'e' => 2, 'o' => 1 }
// vowelCount('Colt') // Map { 'o' => 1 }