Teacher struggling with Hackathon

Link to the project or level:**
**[https://studio.code.org/projects/applab/LHGxo418o4GSiOCoVXcUBvkLz-wZ4mWrQ_3g-afOleY]( https://studio.code.org/projects/applab/LHGxo418o4GSiOCoVXcUBvkLz-wZ4mWrQ_3g-afOleY**
What I expect to happen: I’m not sure if this will even work, but I want to randomly display a dog name and image based upon a list sorted first by size and then by type. I would love to make both filters happen at once, and attempted to base it off of the solution here: Unit 5 - Hackathon - dataset filter from multiple drop downs, but I couldn’t figure out how to have both dropdowns filter in the same function, since I need to have conditionals to establish size.
I thought maybe I could set up an if clause to first match anything that matches the type and then by size, but got nowhere with that.

So ….I first run a size filter and then the type, filtered off of the already filtered size list. I then want to update the screen with a random item from my twice-filtered list.

What actually happens: Nothing happens. It seems to be stuck on my type input dropdown, although I’ve triple-checked the syntax and don’t see that I’ve misspelled anything.

What I’ve tried: I’ve tried commenting out anything referring to the type filter all together to just type if the dog size filter works, but I can’t get that to run either. It keeps getting stuck on the dog type drop down, even when I comment it out.

I would appreciate any help. I’m trying to solve this while recovering from Omicron and my brain is just not wrapping around this at all. Thanks!

First, it looks like you have changed the column names in the database. Is that a plagiarism guard?

First off I would not run it without user input. So delete these lines:

//load the functions?
filterSize();
 findBreed();
 updateScreen();

The second problem is the findBreed() function doesn’t do what you think it does. filteredDogNames already has all the dogs of the proper weight in it. You then add a few more randomly chosen dogs.

You could just && the second requirement in.

function filterBySizeAndType(){
  for(var i=0; i<minWeight.length; i++){
      if(minWeight[i] < 15 && dogSize == "Small" && breed[i] == dogBreed){
        appendItem(filteredDogNames, dogName[i]);
        appendItem(filteredDogImages, dogImage[i]);
        appendItem(filteredDogBreeds, breed[i]);
      
      } else if(minWeight[i] >= 15 && minWeight[i]<50 && dogSize == "Medium" && breed[i] == dogBreed){
         appendItem(filteredDogNames, dogName[i]);
         appendItem(filteredDogImages, dogImage[i]);
         appendItem(filteredDogBreeds, breed[i]);
      } else if(minWeight[i] >= 50 && dogSize == "Large" && breed[i] == dogBreed){
           appendItem(filteredDogNames, dogName[i]);
           appendItem(filteredDogImages, dogImage[i]);
           appendItem(filteredDogBreeds, breed[i]);
    }
  }
}

Your third problem is with initialization. These variables:

var dogSize;
var dogBreed;

have no initial value. They are only given a value if you change the dropdown box. So one or both could be undefined when you press the generate button.

Your fourth problem is in updateScreen().

function updateScreen() {
  setScreen("screen2");
 // Selects a random number then sets the text to that number of the list.
  Random = randomNumber(0, filteredDogNames.length);
  for (var i = 0; i < filteredDogNames; i++) {
    if (filteredDogNames[i] == Random) {
      setText("filteredName", "Dog Breed: " + filteredDogNames[Random]);
      setImageURL("filteredImage", filteredDogImages[Random]);
    }
  }
} 

You have this filteredDogNames[i] == Random will that ever be true? Let me be clear is "Akita" == 5 ever going to be true?

Besides, you don’t need to search if you already have the index. Why not just do this

setText("filteredName", "Dog Breed: " + filteredDogNames[Random]);
setImageURL("filteredImage", filteredDogImages[Random]);

and be done with it.

Your last problem is also in updateScreen(). Your random number can be one larger than the last index. So just subtract one Random = randomNumber(0, filteredDogNames.length-1);

Bonus problem: What do you do if the filtered list is empty?

1 Like

Thanks jdonwells! You are the bee’s knees. Among my many problems, my main one is that I don’t really know what I’m doing, but that’s why I am trying to figure it out and attempting to work my way through problems. I appreciate your help and all the answers that you have provided in this forum. I will make your suggested changes and work on what to do when my list is empty. I know that the size sort will work, but I’m not sure about the breed. I guess I could test it on its on and make sure that I can sort on those keywords. Thanks again!

1 Like