+ 1

Help with excercise Distinct values of a set

Hi, I ran can't get right the exercise from Set in ES6. Could you please try to help me to find a mistake in my code? Thanks a lot in advance!!!! Here is the task: You are making a program for a sports store warehouse. The warehouse currently has 7 types of sports equipment and plans to purchase more. The program you are given takes 3 new purchased item types as input. let products = new Set(["dumbbells", "barbell", "t-shirt", "swim short", "gloves", "training apparatus", "goggle"]); NODE Write a program to add the new items to the given set, then calculate and output to the console the count of item types in the warehouse. Sample Input barbell gloves bandage Sample Output 8 Explanation Before the purchase, we already had a "barbell" and "gloves", but did not have a "bandage". So +1 to our item types: 7+1 = 8. Now is the right time to use size property. Here is my solution, which passes only the last test case, but for some reason not all of them. function main() { let products = new Set([ "dumbbells", "barbell", "t-shirt", "swim short", "gloves", "training apparatus", "goggle" ]); var count = 0; while(count<3){ var itemType = readLine(); //add the item to the set count++; let products = new Set() products.add("itemType1").add("itemType2").add("itemType3"); } //calculate and output the count of item types console.log(products.size); }

23rd Jul 2021, 2:13 PM
Brigi
Brigi - avatar
4 odpowiedzi
+ 5
Look inside the while...loop 1. You create a local `Set` object with similar name <products>. let products = new Set(); // new Set object created * Suggestion: Remove that line. 2. You added "itemType1", "itemType2", "itemType3" to the local `Set` object <products> products.add("itemType1").add("itemType2").add("itemType3"); But this does NOT affect the `Set` object <products> defined outside the loop. The `Set` object defined outside the loop still has only 7 items as the loop ends. * Suggestion: Also remove that line. 3. You should instead, add <itemType>, which is the input from user. It might also be considered, to switch <itemType> to lowercase, in case different alphabet case fails the unique check. But it's just an idea, not part of the task, thus can be abandoned. 4. Fixed snippet: var count = 0; while( count < 3 ) { var itemType = readLine(); // add the item to the set products.add( itemType ); // add <itemType> to Set count++; }
23rd Jul 2021, 2:53 PM
Ipang
+ 2
Thanks a lot @Ipang!
23rd Jul 2021, 3:02 PM
Brigi
Brigi - avatar
+ 1
Pariket I tried and it doesn't pass any test cases instead of passing at least one.
23rd Jul 2021, 2:35 PM
Brigi
Brigi - avatar
+ 1
@Patiket I keep getting 7 all the time, it seems to me that the issue lays in adding new items in an existing Set
23rd Jul 2021, 2:44 PM
Brigi
Brigi - avatar