Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

251
Visualizações
How to loop through values within an array, and display them onto the page, each time?

I am currently on The Library project step 3:

Write a function that loops through the array and displays each book on the page. You can display them in some sort of table, or each on their own “card”.

I am seeing errors. Within my displayBooks() function, I am using a .forEach method to loop through each book pushed into myLibrary array and set paragraphs to the values, I am then using a styleBooks() function to give these paragraphs classes for styling. Then in addBookToLibrary(), I am calling these functions. . After I fill out a form and submit I notice my card is blank, once I fill out a form again, the computer isn't making individual cards for each book and is only overriding the first card. How can I make it this isn't the case? Full code(https://codepen.io/nolimitz71/pen/WNXGrjP)

let myLibrary = [

];

function Book(title, author, pages, read) {
  this.title = title;
  this.author = author;
  this.pages = pages;
  this.read = read;
}

function displayBooks() {
  myLibrary.forEach((book) => {
    para1Text.innerHTML = book.title;
    para2Text.innerHTML = book.author;
    para3Text.innerHTML = book.pages;
    para4Text.innerHTML = book.read;
  })
}

function styleBooks() {
  para1.classList.add('para-title');
  para1.appendChild(para1Text);
  bookOutline.appendChild(para1);

  para2.classList.add('para-author')
  para2.appendChild(para2Text);
  bookOutline.appendChild(para2);

  para3.appendChild(para3Text);
  para3.classList.add('para-pages')
  bookOutline.appendChild(para3);

  para4.appendChild(para4Text);
  para4.classList.add("para-status")
  bookOutline.appendChild(para4);
}

function addBooktoLibrary() {
  let newBook = new Book(bookTitle.value, bookAuthor.value, bookPages.value, bookOptions.value);
  myLibrary.push(newBook);
  displayBooks()
  styleBooks()
  bookForm.onsubmit = e => {
    e.target.reset();
    bookValues.style.display = "block";
  };
  bookForm.style.display = "none";
  console.log(myLibrary)
}
bookSumbit.addEventListener("click", addBooktoLibrary);

Error Image

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try to replace const with let. Const Variables cannot be modified

Current:

bookSumbit.addEventListener("click", addBooktoLibrary);

Try:

let bookSubmit = document.querySelector(".submit");
bookSubmit.addEventListener("click", addBooktoLibrary);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda