Help Finding the Error in the Code

I am missing something and can’t seem to figure it out … I have tried changing so many things at this point. I have been changing up how I do my class since there are so many videos out there showing how to do stuff so I gave my students an empty project of the Random Dog Picker and had them write the code… I am missing where the error is with this one as to why it isn’t working.

Also, this one…

i just found it better to rewrite the project this way

var Data = readRecordsSync("Dogs");
// var Pics = [(getColumn("Dogs", "Image"))];
// var DogBreed = [(getColumn("Dogs", "Name"))];
// var Size = [(getColumn("Dogs", "Maximum Height"))];
// var Dogsize = getText("sizeDropdown");
// var FilteredDogBreed = [];
// var FilteredPics = [];
onEvent("sizeDropdown", "change", function (e) {
  updateUI(getText(e.srcElementId))
  // updateScreen();
  // updateScreen2();
});
function updateUI(choice) {
  var options = [];
  for (var i = 0; i < Data.length; i++) {
    var dog = Data[i];
    var size = dog["Maximum Height"];
    if (choice === "Small" && size < 16 || choice === "Medium" && size >= 16 && size < 24 || choice === "Large" && size > 24) {
      options.push({image: dog.Image, size: size, breed: dog.Name})
    }
  }
  var randomDog = options[randomNumber(0, options.length-1)];
  setImageURL("picture", randomDog.image);
  setText("nameOutput", randomDog.breed);
}
// function updateScreen() {
// FilteredDogBreed = [];
// FilteredPics = [];


// for (var i = 0; i < DogBreed.length; i++) {
//     if (Size[i]<16 && Dogsize == "Small") {
//       appendItem(FilteredDogBreed, DogBreed[i]);
//       appendItem(FilteredPics, Pics[i]);
//     } else if ((Size[i] >= 16 && (Size[i] < 24 && Dogsize == "Medium"))) {
//       appendItem(FilteredDogBreed, DogBreed[i]);
//       appendItem(FilteredPics, Pics[i]);
//     } else if ((Size[i]>=24 && Dogsize == "Large")) {
//       appendItem(FilteredDogBreed, DogBreed[i]);
//       appendItem(FilteredPics, Pics[i]);
//     }
//   }
// }
// function updateScreen2() {
//   var indexv = "";
//   indexv = randomNumber(0, FilteredDogBreed.length-1);
//   setImageURL("picture", FilteredPics[indexv] + "");
//   setText("nameOutput", FilteredDogBreed[indexv] + "");
// }

I try to show them what they did wrong rather than rewrite the entire program. Thanks for trying.

HI @tdean1,

This project is from AP Computer Science Principles so out of scope for Discoveries. Have the kids covered the topics of lists, loops and traversals?

Here are some tips to try and get you all “unstuck” as well as a link to a solution if you really need help…

The first app has an error on the very first line. My CSP student often made this mistake. When you import a data table, you don’t need the square brackets. I didn’t catch it at first but when I went to test the length of DogBreed list by using console.log(DogBreed.length) - it was 1 but should have been 105. So make sure the table/columns are imported correctly.

The second big cause for error for both apps is the way that the dogSize variable is updated and used. Remember, you want the size variable to:

  • Update every time it changes
  • Be accessible by the function that needs it (so either declared in the function OR a global variable).

I believe both of the apps above capture size just one time when the app is initially run but not each time it changes with the dropdown.

HERE is the link to the activity in the CSP curriculum. It does have working code so if you want to try and solve using the tips above first - wait before you take a look :wink:.

~Michelle

fair enough, though there was more than just 1 thing wrong with it (at least when i was troubleshooting)… so i just took the liberty to remake it from scratch, my version also allows more flexibility since you could just pass through the whole instance if you wanted more data later in the future and uses less indexing definitely recommend Melynn’s advice though