-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmap.html
More file actions
59 lines (53 loc) · 1.68 KB
/
Copy pathmap.html
File metadata and controls
59 lines (53 loc) · 1.68 KB
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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title> Using a Map to track dogs </title>
<script>
let breeds = new Map();
breeds.set("Mixed", 3);
breeds.set("Poodle", 2);
breeds.set("Chihuahua", 1);
class Dog {
constructor(name, breed, weight) {
this.name = name;
this.breed = breed;
this.weight = weight;
}
bark() {
console.log(`${this.name} says Woof Woof!`);
}
}
function countDogsByBreed(dogs) {
const dogBreedMap = new Map();
dogs.forEach(dog => {
let count = dogBreedMap.get(dog.breed) || 0;
dogBreedMap.set(dog.breed, count+1);
});
return dogBreedMap;
}
let fido = new Dog("Fido", "Mixed", 35);
let spot = new Dog("Spot", "Chihuahua", 10);
let fluffy = new Dog("Fluffy", "Poodle", 30);
let rover = new Dog("Rover", "Mixed", 29);
let max = new Dog("Max", "Poodle", 22);
let frankie = new Dog("Frankie", "Mixed", 38);
let dogsInShow = [ fido, spot, fluffy, rover, max, frankie ];
let breedMap = countDogsByBreed(dogsInShow);
breedMap.forEach((count, breed) =>
console.log(`There are ${count} dogs of ${breed} breed in the show`)
);
console.log("Number of poodles in the show:", breedMap.get("Poodle"));
console.log("Number of different breeds in the show:", breedMap.size);
console.log("Does the show include Mixed breed dogs?",
breedMap.has("Mixed") && breedMap.get("Mixed") > 0);
dogsInShow = [ fido, spot, fluffy, rover, max ];
let dogsSet = new Set(dogsInShow);
dogsSet.add(fido); // doesn't change dogsSet because fido is already there
dogsSet.add(frankie); // adds frankie to the set
dogsSet.delete(max); // removes max from the set
dogsSet.forEach(dog => console.log(dog.name)); // fido, spot, fluffy, rover, frankie
</script>
</head>
<body> </body>
</html>