Skip to content

Commit d769320

Browse files
committed
added mapSetExercise.js
1 parent 0fd0ccb commit d769320

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

mapSetExercise.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/////////Quick Question #1////////////////////
2+
//What does the following code return?
3+
// new Set([1,1,2,2,3,4])
4+
5+
{1,2,3,4}
6+
7+
/////////Quick Question #2////////////////////
8+
//What does the following code return?
9+
// [...new Set("referee")].join("")
10+
11+
"refere"
12+
13+
/////////Quick Question #3////////////////////
14+
//What does the Map m look like after running the following code?
15+
// let m = new Map();
16+
// m.set([1,2,3], true);
17+
// m.set([1,2,3], false);
18+
19+
// m {
20+
// [1,2,3]: true,
21+
// [1,2,3]: false,
22+
// }
23+
24+
25+
///////////////////////**hasDuplicate**///////////////////////////////
26+
//Write a function called hasDuplicate which accepts an array and returns true or false if that array contains a duplicate
27+
hasDuplicate([1,3,2,1]) // true
28+
hasDuplicate([1,5,-1,4]) // false
29+
30+
function hasDuplicate(arr) {
31+
if (arr.filter(
32+
(val) => (!(new Set(arr)).has(val)))
33+
) {
34+
return false
35+
};
36+
return true;
37+
}
38+
39+
///////////////////////**vowelCount**///////////////////////////////
40+
41+
//Write a function called vowelCount which accepts a string and returns a map where the keys are numbers
42+
//and the values are the count of the vowels in the string.
43+
//vowelCount('awesome') // Map { 'a' => 1, 'e' => 2, 'o' => 1 }
44+
//vowelCount('Colt') // Map { 'o' => 1 }
45+
46+
const isVowel = (letter) => "aeiou".includes(letter);
47+
48+
function vowelCount (string) {
49+
let newMap = new Map();
50+
let smallChar = string.toLowerCase();
51+
52+
for(let char of smallChar) {
53+
if (isVowel(char)) {
54+
if (newMap.has(char)) {
55+
(newMap.set(char, newMap.get(char)+1));
56+
}
57+
else {newMap.set(char, 1)};
58+
}
59+
};
60+
return newMap;
61+
}

0 commit comments

Comments
 (0)