Hackathon List Filtering Help

[Posts in the debugging category need to have the following pieces of information included in order to help other teachers or our moderator team best help you. Feel free to delete everything in brackets before posting your request for debugging help.]

Student Code

The current code creates a filtered list that only stores albums from the chosen genre as well as a separate list that only stores the artists of those albums. It uses the same code to make a filtered list that only stores albums from the chosen decade as well as a separate list that stores the artists of those albums.

The code then filters the two lists with albums to only add the albums they have in common to a new list. The student wants to randomly choose an album from this list and also display the corresponding artist.

We can’t figure out a way to filter the two lists with artists so that it corresponds perfectly to the list that combines the albums. I understand this sounds confusing but it’s more clear when you see the code.

Please keep in mind that we’re still trying to keep the code simple!

Here’s some code I made real quick - it’s very simple and I labeled everything going on with it. Instead of filtering through the albums twice, you check if both the values match in the same “if” statement.
Right now the code only displays the album by showing it in the console, but I bet you or your student can figure out how to show it on the screen!
Let me know if you have any questions :slight_smile:

var filteredAlbums = [];

function combine(){
  //Get inputs as number and text
  decade = getNumber("decadeInput");
  genre = getText("genreInput");
  //Loop through every album
  for(var i=0;i < genreList.length; i++){
    //Check if the genres match and if the decade is correct
    if(genreList[i] === genre && yearList[i] >= decade && yearList[i] < decade+10){
      //Append the album index -- will use this for later
      appendItem(filteredAlbums, i);
  //Check if it found any albums that fit the pairing
  if(filteredAlbums.length === false){
    return console.log('No albums found with that pair!');
  //Find a random album from the list we created
  var randNum = randomNumber(0, filteredAlbums.length);
  var randAlbum = filteredAlbums[randNum];
  //Display it! :)
  console.log(yearList[randAlbum], albumList[randAlbum], genreList[randAlbum], artistList[randAlbum]);
1 Like