Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

102
Views
Local storage update quantities

i have a question i've make this code to update my local storage, so everything work but when i update my local storage to add quantities, for exemple if i have already 2 couch and i add 2 more couch in my local storage the result is 22

So i think it miss some "parseInt" in my code but i don't realy know where so please that will be greatfull if you can help me i don't really know what to do

btnCart.addEventListener("click", (event)=>{


  event.preventDefault();

  function addToCart() {


    let color = colorChoice.value;
    console.log(color);
  
    let name = productName;
    console.log(name);
  
    let price = productPrice;
    console.log(price);
  
    let quantity = productQuantity.value;
    console.log(quantity);

  
    const productToAdd = {
      id : id,
      name : name,
      price : price,
      color: color,
      quantity: quantity
    };


    const currentCart = JSON.parse(localStorage.getItem(cartName)) ?? [];


    const itemIndex = currentCart.findIndex((item) =>
            item.id === productToAdd.id && item.color === productToAdd.color
    );

    if (itemIndex !== -1) {

        currentCart[itemIndex].quantity += productToAdd.quantity;
    } 
    
    else {
    
        currentCart.push(productToAdd);
    }


    localStorage.setItem(cartName, JSON.stringify(currentCart));
}
addToCart();


})

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

LocalStorage return strings, so you can't do:

currentCart[itemIndex].quantity += productToAdd.quantity;

You need to parse the localStorage value eg:

const qty = parseInt(currentCart[itemIndex].quantity);
currentCart[itemIndex].quantity = qty + productToAdd.quantity;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!