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

155
Visualizações
Simple JS program 3 item with negative and positive click boxes and total

Just started to learn javascript. I have a simple school project. I have 3 items with - and + buttons with total for each and then final total for all 3. I can't get the total button to not go below 0. It goes to negative values. The other 3 don't drop below 0 in their individual total count.

//set default values

let gb = 0 // Gingerbread
let cc = 0 // Chocolate Chip
let ss = 0 // Sugar Sprinkle
let total = 0

//set variables for quantity

var totalQuantity = document.getElementById('qty-total');
var gbQuantity = document.getElementById('qty-gb');
var ccQuantity = document.getElementById('qty-cc');
var ssQuantity = document.getElementById('qty-sugar');
//set event listener for Gingerbread cookie + button
document.getElementById("add-gb").addEventListener("click", function(e) {
{
    gb++
}
gbQuantity.textContent = gb;
total = total + 1;
totalQuantity.textContent = total;
console.log("Gingerbread + was clicked!")
})

// set even listener for Gingerbread cookie - button
document.getElementById("minus-gb").addEventListener("click", function(e) {
if(gb > 0)
{
     gb--
} 
   
gbQuantity.textContent = gb;

total = total - 1;
totalQuantity.textContent = total;
        
console.log("Gingerbread - was clicked!")
})

//set event listener for Chocolate chip cookie + button

document.getElementById("add-cc").addEventListener("click", function(e) {

{
    cc++
}

ccQuantity.textContent = cc;

total = total + 1;
totalQuantity.textContent = total;

console.log("Chocolate Chip + was clicked!")
})

//set event listener for Chocolate chip cookie - button

document.getElementById("minus-cc").addEventListener("click", function(e) 
{
if(cc > 0)
{
    cc--

}
    
ccQuantity.textContent = cc;
total = total - 1;
totalQuantity.textContent = total;

console.log("Chocolate Chip - was clicked!")
})

//set event listener for Sugar Sprinkle cookies + button

document.getElementById("add-sugar").addEventListener("click", function(e) {

{
    ss++
}

ssQuantity.textContent = ss;
totalQuantity = document.getElementById("qty-total");
total = total + 1;
totalQuantity.textContent = total;

console.log("Sugar Sprinkle + was clicked!")
})

//set event listener for Sugar Sprinkle cookies - button

document.getElementById("minus-sugar").addEventListener("click", function(e) {
if ( ss > 0)
{
    ss--
}    

ssQuantity.textContent = ss;

total = total - 1;

totalQuantity.textContent = total;

console.log("Sugar Sprinkle - was clicked!")
})

So the total quantity keeps dropping below 0 after the minus buttons are still clicked. Thank you for any assistance.

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

0

Without seeing all your code, one way to achieve this would be to wrap a condition around all parts of your code total = total - 1; which decrement total....

if(total >= 1){
  total = total - 1;
}

So now, total will only decrease if it is currently a value greater than or equal to 1.

about 4 years ago · Juan Pablo Isaza Relatório

0

I think you need to initialize the total variable with this at start of code. This will ensure that total starts with the correct sum.

total = totalQuantity.textContent

And it should be ensured that the qty-total in UI should be set to qty-gb + qty-cc + qty-sugar


Alternatively, you could also do this at the start:

total = gbQuantity + ccQuantity + ssQuantity

about 4 years ago · Juan Pablo Isaza Relatório

0

There's at least two simple fixes to consider:

  1. Just make sure that the if-check curly brackets in your substraction functions wrap around not only cc--, gc-- and ss--, but also make sure that the logic that substracts from the total (i.e., these lines - total = total - 1) are also inside the same if-condition. Otherwise currently you're substracting from the total, no matter if you substracted from the gc, ss or cc

OR

  1. Instead of doing calculations with the total like total = total + 1 or total = total - 1, you could always just say that total = gc + cc + ss, for example. :)
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