0

Distinct values confusion

Why is the code not seeing the additional items? function main() { let products = new Set([ "dumbbells", "barbell", "t-shirt", "swim short", "gloves", "training apparatus", "goggle", ]); var count = 0; while(count<3, count++){ var itemType = readLine(); //add the item to the set products.add(itemType); } //calculate and output the count of item types for (let count of set.products()) console.log(products.size); }

29th Apr 2021, 11:56 AM
Lee 4 Code
Lee 4 Code - avatar
3 ответов
+ 4
The while-loop is mistakenly constructed. while( count < 3 ) { // code here count++; } Or alternatively, while( count++ < 3 ) { // code here } I'm not sure what you're trying to do with the for-each loop at the bottom. As I understand it, `Set` stores unique data, so every `Set` entry should be unique (no duplicates). I don't think it is a good idea to use a `Set` for counting frequency of data. (Edited)
29th Apr 2021, 12:24 PM
Ipang
+ 2
Thank Ipang! I tried the first loop from the previous example as a shot in the dark. ✔
29th Apr 2021, 1:04 PM
Lee 4 Code
Lee 4 Code - avatar
+ 2
Keep trying and good luck! 👍
29th Apr 2021, 1:45 PM
Ipang