I was checking the Example Solution for the Netflix App and I noticed that the list of recommendations given by the app can contain the same movie/or TV show more than once.
I believe this happens because the searchTypeList function uses randomNumber to select 5 items (we run the loop 5 times) from the filteredTitles list, and it is possible to get the same number multiple times.
I thought this can be easily fixed by adding one line of code inside the second loop, right on the bottom. This is: removeItem(filteredTitles, index);
With this change, after each item is selected from the list and added to the variable output, it is removed from the list. Thus, there is no way we can get it again.
The next time we click the pickBtn, we call the searchTypeList function and the list gets populated again.
I have run the app several times and I think this works.
Great simple addition! I’m going to use this as a check for understanding/extension for my students to review list manipulation.
More on the Netflix App
Well, it turns to out that for several of the combinations typeInput - ratingInput, we get either empty lists or lists containing only one item. This happens when selecting the TV Show option in combination with the following ratings: G, PG, PG-13, and R.
If we use the example solution we will see a message with 5 “undefined” titles if the filteredTitles list is empty, or a message with the same 5 titles, if the filteredTitles list has only one item.
I had suggested to add removeItem(filteredTitles, index); to avoid having repeated titles among the 5 suggestions. This works well if the list has 5 or more than 5 items. If the list has less than 5 items, we will get an error message. This is because we are trying to remove items from a list that does not have them.
So, here is the new approach.
I created a series of conditionals to check the length of the filteredTitles list. Based on this, I generate different messages and I use, or don’t use, the randomNumber and removeItem commands.
- If filteredTitles is empty, the screen element picksOutput will show a message saying that there are no TV Shows (or Movies) that match the selected criteria.
- If filteredTitles contains between 1 and 4 items-titles, the message in the screen will say that there are not many TV Shows (or Movies) meeting the selected criteria, and it will list the titles.
- If filteredTitles contains 5 items-titles, the message in the screen will say that there are only 5 TV Shows (or Movies) that match the selected criteria, and it will list the titles.
- Finally, if filteredTitles contains more than 5 items-titles, the message will say that the following are the TV Shows (or Movies) meeting the criteria, and it will list them.
RandomNumber and removeItem are only used in the last case. Since there are more than 5 items-titles in the list, I want to randomly choose them. I also want to remove the items after I have chosen them, so I do not have repeated ones. I don’t need to do either of these if we have 5 or less items in the list. In fact, if we do that, we will get error messages in the console and/or silly messages in the app screen, like 5 “undefined” titles.
I still use loops when the filteredTitles list contains 5 or less items, so I am able to attach a number in front of each title when listing them in the app screen.
This is the link to this new extended version of the app.
See you all.