Student created Webpage help!

Hello! I need help fixing a student’s issue…see below…

The purpose of this app is to help the user choose what type of dog they should adopt based on size and breed group. After the user chooses the size and breed group and clicks enter, the app will display a random name and random picture that match the size and breed group they have chosen in the previous screen. But sometimes the result is “undefined”. Like if you choose “Small” and “Toy”, the result will be undefined since there isn’t a dog breed from the table that matches both of the requirements.

So what I’m trying to do is make the screen display “Sorry. Please choose again” whenever the result is undefined. This is the code the student wrote, but it does not work:

var output = getText(“dogOutput”);
if(output != filteredNames[randomIndex]){
setText(“dogOutput”, “Sorry. Please try again.”);

Here is a link to the student’s app:

First, look at line 82 what should be added to filteredImages?

Lines 57 and 58. What is displayed in "dogOutput" if you set it then set it a second time?

Line 61. You are looking at the output of the code you just ran. What if you knew what the code would produce before you run it? How could you know that there are no suitable dogs in the filtered list? Could you use filteredNames.length somehow to know when there are no choices?

Big over all question here: Did you mean to always select a dog by Breed Group and ignore size or did you want to apply both size and breed together? Because right now you only choose from the breed group.

Sorry about all the questions. I have been hanging out with this guy Socrates too much I think.

1 Like

The student meant to apply both the size and breed together.
Updated version:

They made it so that it will display two results. Is there a way to apply both size and breed group and still have only one result?

There are two ways to do that.

One way is to combine both the size and breed into the conditions in the filter function using &&.

Another way is to take the resulting arrays used in filter as input to breedFilter. So, filter for size then take the size filtered list of dogs and then filter for the breed. That yields a single list of dogs that meet both criteria. Right now you start both filters from the full list and end up with two different lists.

In either case the doublely filtered list can be empty.

1 Like

Thank you for the help!!