Quick Question 1
What does the following code return?
new Set([1,1,2,2,3,4])
//(4) [1, 2, 3, 4]
//Made a function for this:
const question_1 = function() {
const set = new Set([1, 1, 2, 2, 3, 4]);
return Array.from(set);
}
Quick Question 2
What does the following code return?
[...new Set("referee")].join("")
//"ref"
//Made a function for thescript
const question_2 = function() {
const result = [...new Set("referee")].join("");
return result;
//"ref"
}
Quick Question 3
What does the map m? look like
let m = new Map();
m.set([1,2,3], true);
m.set([1,2,3], false);
//Map(2) {(3) [1, 2, 3] => true, (3) [1, 2, 3] => false}
//Made a function for this
const question_3 = function() {
let m = new Map();
m.set([1,2,3], true);
m.set([1,2,3], false);
return m;
//Map(2) {(3) [1, 2, 3] => true, (3) [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
const hasDuplicate = function(arr) {
const set = new Set(arr);
return set.size !== arr.length;
}
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.
const vowelCount = function(str) {
const vowels = new Map([
['a', 0],
['e', 0],
['i', 0],
['o', 0],
['u', 0]
]);
const lowStr = str.toLowerCase();
for(element of Array.from(lowStr)) {
let vowel = vowels.get(element);
if(vowel != undefined) {
vowels.set(element, ++vowel);
}
};
//Now remove unused vowels
vowels.forEach((value, key) => {
if(value === 0)vowels.delete(key);
});
return vowels;
}
thescript.test.js
describe("hasDuplicate(Array)", () => {
it("Returns true if the array has duplicate, otherwise false", () => {
expect(hasDuplicate([1,3,2,1])).toEqual(true);
expect(hasDuplicate([1,5,-1,4])).toEqual(false);
})
});
describe("vowelCount(String)", () => {
it("Returns a map where the keys are vowles and the values are the number of occurences", () => {
let m1 = new Map([['a',1],['e',2],['o',1]]); //Vowels for "Awesome"
let m2 = new Map([['o',1]]); //Vowels for "Awesome"
expect(vowelCount('awesome')).toEqual(m1);
expect(vowelCount('Colt')).toEqual(m2);
})
});
Jasmine Testing