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

140
Visualizações
How an I Put a sum in the localstorage?

I want to put the sum in my localStorage thanks to a function because, I have to work with the creation of function. When I put the total of my basket in my local storage, the nav changes number. So, when refreshing the page, the total is counted as one more article and I have an error on the html tag "nan".Can you explain to me what is wrong with my code ?

    let totalityPrice = document.querySelector('.subtotal');
    let products = [];
    let Total = 0;

    function displayProduct() {
      if (localStorage.length > 0) {
        for (let key in localStorage) {
          let product = JSON.parse(localStorage.getItem(key));
          document.querySelector('.cart span').textContent = localStorage.length;
          if (product) {
            products.push(key);
            cartTablebody.innerHTML += `

    <tr>
    <td>${product.title}</td>
    <td>${product.price / 100}</td> //price=API data//
    </tr> 

    `;
            Total += product.price / 100;
          }
        }
      }
    }
    displayProduct();

    function calculatePrice() {
      totalityPrice.innerText = Total;
      console.log(Total);
      //localstoragesetItem//
    }
    calculatePrice();

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

0

It sounds like there's something in localStorage that isn't a cart item. Check that each item has all the required properties before processing it.

And instead of using localStoage.length as the product count in .cart span, use products.length.

let totalityPrice = document.querySelector('.subtotal');
    let products = [];
    let Total = 0;
    function displayProduct() {
      if (localStorage.length > 0) {
        for (let key in localStorage) {
          let product = JSON.parse(localStorage.getItem(key));
          
          if (product && "title" in product && "price" in product) {
            products.push(key);
            cartTablebody.innerHTML += `

    <tr>
    <td>${product.title}</td>
    <td>${product.price / 100}</td> //price=API data//
    </tr> 

    `;
            Total += product.price / 100;
          }
        }
      }
    }
    displayProduct();

    function calculatePrice() {
      totalityPrice.innerText = Total;
      console.log(Total);
      document.querySelector('.cart span').textContent = products.length;;
    }
    calculatePrice();

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