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

138
Visualizações
local storage in browser not incrementing?

I'm trying to build a date booking feature that returns the total cost of a booking based on the dates selected and the cost associated with that booking.

I have converted the dates into milliseconds and I'm looping through the possible date ranges that costs are assigned to (15th dec - 15th jan = 220 dollars per night for example).

I then want to store in local storage the count of times that a date falls within a particular date range so I can just do the math at the end of multiplying the number of times dates fall within a date range and multiple by that cost.

My issue is i'm not sure if I am correctly incrementing the count in the local storage as shown in the screenshot below

for (let i = 0; i < daysDifference; i++) {  //for loop to go through each day
    let dayDot = startTime + (i * 86400000); // getting the range for each day of the booking in millisecs
    localStorage.setItem("dayscost0", "0") //setting day count in each range to zero in local storage
    if (dayDot <= 1639746000000 && dayDot >= 1634648400000) { // setting range for price blocks
      let cost = (parseInt(localStorage.getItem('dayscost0')) + 1); //incrementing by one each time a value fits the range
      localStorage.setItem("dayscost0", cost.toString()) //adding the value to local storage
      console.log('dayscost0') 
    }

browser Console image

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

0

Problem is thers is always reinitialization - localStorage.setItem("dayscost0", "0")on each iteration. Try moving it outside of the for-loop.

localStorage.setItem("dayscost0", "0") //setting day count in each range to zero in local storage
for (let i = 0; i < daysDifference; i++) {  //for loop to go through each day
    let dayDot = startTime + (i * 86400000); // getting the range for each day of the booking in millisecs
    if (dayDot <= 1639746000000 && dayDot >= 1634648400000) { // setting range for price blocks
      let cost = (parseInt(localStorage.getItem('dayscost0')) + 1); //incrementing by one each time a value fits the range
      localStorage.setItem("dayscost0", cost.toString()) //adding the value to local storage
      console.log('dayscost0') 
    }
}

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