From the course: JavaScript: Maps and Sets
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
Solution: Sets 1 - JavaScript Tutorial
From the course: JavaScript: Maps and Sets
Solution: Sets 1
(upbeat music) - [Instructor] In this challenge, we're given an array of pet names and we need to create a new set based on this array. So the first thing that we'll need to do is actually establish our new set. So I want to point out from the beginning that there could be a couple different ways that you achieve the correct solution in this particular challenge. My way is not the only way. So keep that in mind when you are working through this. So the first thing that I'm going to do is I'm going to create a new variable and I'm going to call it petSet, and I'm going to set that equal to a new Set. Then I know I want to add each item that's in this namesForFamilyPet array. I want to add it to this new set. So what I'm going to do is actually iterate over this array, and, for each name, I'm going to add it to the set. So we will actually utilize our array methods here. So namesForFamilyPet and we're going to to use forEach here. And I'm going to say forEach(name) and I'm going to…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
The Set object defined57s
-
(Locked)
Add values to your set3m 17s
-
(Locked)
Does Set have your value?3m 9s
-
Get your set size2m 2s
-
(Locked)
Delete and clear values in your set2m 46s
-
(Locked)
Iterate over values in a set2m 45s
-
(Locked)
Iterate over a set with entries2m 25s
-
(Locked)
Iterate over your set with forEach2m 29s
-
(Locked)
Solution: Sets 12m 8s
-
(Locked)
Solution: Sets 22m 35s
-
-
-