Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Why won't it print multiple cards

I'm trying to get it this program to when you type in the search bar something that relates to 2 or more books you get all of those books printed out. For some reason it only prints the last book that it finds(through the booksFiltered function) when i console the booksFiltered array it says it has 2 books in it so I don't understand why it only prints the last book. Sorry if the code is a bit messy. The main part to focus on is the filterBooks and renderBooks functions.

// TODO: Add genres and when typed shows a list of bookSearch
// TODO: Add books to array
// TODO: Add a "rent" button to books
const bookSearch = document.getElementById('bookSearch');
const searchTitle = document.getElementById('searchTitle');
const searchButton = document.getElementById('searchButton');
//toggles image
var i;
var searchVal = bookSearch.value;
var recBooks = [{
    title: 'DogMan',
    author: 'Dav Pilkey',
    rec: true,
    description: 'From the creator of captain underpants DogMan is a comic about a dog turned superhero in colorful fashion.',
    keyOne: 'dog',
    keyTwo: 'animal hero',
    genre: 'funny'
  },
  {
    title: 'DogMan Unleashed',
    author: 'Dav Pilkey',
    rec: true,
    description: 'This is the second part of the amazing DOGMAN series!',
    keyOne: 'dog 2',
    keyTwo: 'animal hero',
    genre: 'funny'
  }
];
var booksFiltered = [];
var recsFiltered = [];

const print = (message) => {
  var outputDiv = document.getElementById('books-wrap');
  outputDiv.innerHTML = message;
}
function alert() {
  console.log("hi");
  this.render = function(dialog) {
    var winW = window.innerWidth;
    var winH = window.innerHeight;
    var dialogoverlay = document.getElementById('dialogoverlay');
    var dialogbox = document.getElementById('dialogbox');
    dialogoverlay.style.display = "block";
    dialogoverlay.style.height = winH + "px";
    dialogbox.style.left = (winW / 2) - (550 * .5) + "px";
    dialogbox.style.top = "100px";
    dialogbox.style.display = "block";
    document.getElementById('dialogboxhead').innerHTML = "Acknowledge This Message";
    document.getElementById('dialogboxbody').innerHTML = dialog;
    document.getElementById('dialogboxfoot').innerHTML = '<button onclick="Alert.ok()">OK</button>';
    const input = document.getElementById('input');
    const signButton = document.getElementById('sign');
  }
  this.ok = function() {
    document.getElementById('dialogbox').style.display = "none";
    document.getElementById('dialogoverlay').style.display = "none";
  }
}

var Alert = new alert();

function filterTheBooks() {
  var found = false;
  if (bookSearch.value == "") {
    console.log("Empty");
    booksFiltered = [];
    renderBooks();
    Alert.render('THAT IS NOT A VALID INPUT.');
  } else {
    var found = false;
    for (var i = 0; i < books.length; i++) {
      var currentBook = books[i];
      if (bookSearch.value === currentBook.title || bookSearch.value === currentBook.keyOne || bookSearch.value === currentBook.keyTwo || bookSearch.value === currentBook.genre) {
        found = true;
        booksFiltered.push(currentBook);
        renderBooks();
      }
    }
    if (found) {
      console.log("Found");
    } else {
      console.log("We either do not have the book or you mistyped the title.");
      renderBooks();
      bookSearch.value = "";
      Alert.render('THAT IS NOT A VALID INPUT.');
    }
  }
}

function renderBooks() {
  for (var i = 0; i < booksFiltered.length; i++) {
    var currentBook = booksFiltered[i];
    let booksHTML = '<div class="card" id="book-' + i + '"><div class="container"><h4><b class="bookTitle" id="bookTitle">' + currentBook.title + '</b></h4><p class="bookDesc" id="bookDesc">' + currentBook.description + '</p><img src="http://icons.iconarchive.com/icons/graphicloads/colorful-long-shadow/256/Book-icon.png" class="bookPic" id="bookPic"><p class="author" id="author">' + currentBook.author + '</p></div></div>';
    message = booksHTML;
    print(message);
  }
}
function recsRender() {
  var booksHTML = "";
  for (var i = 0; i < recsFiltered.length; i++) {
    var currentBook = recsFiltered[i];
    booksHTML += '<div class="recCard" id="recBook-' + i + '"><div class="recContainer"><h4><b class="recBookTitle" id="recBookTitle">' + currentBook.title + '</b></h4><p class="recBookDesc" id="recBookDesc">' + currentBook.description + '</p><img src="http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/256/Actions-rating-icon.png" class="recBookPic" id="recBookPic"><p class="recAuthor" id="recAuthor">' + currentBook.author + '</p></div></div>';
  }
  document.getElementById('recBooks-wrap').innerHTML = booksHTML;
}

function filterTheRecs() {
  recsFiltered = [];
  for (var i = 0; i < recBooks.length; i++) {
    var currentBook = recBooks[i];
    if (currentBook.rec) {
      recsFiltered.push(currentBook);
    }
  }
}
filterTheRecs();
recsRender();
searchButton.addEventListener('click', () => {
  filterTheBooks();
});
renderBooks();

Edit: Thanks for the help. I knew that was the problem but I went about the wrong way to fix it. After I saw your answer I decided to delete the function all together and do something else which worked. Thanks again!

1 Answer

It's your print function. It's overwriting the innerHTML each time you print something.

const print = (message) => {
  var outputDiv = document.getElementById('books-wrap');
  outputDiv.innerHTML = message;
}