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

218
Visualizações
how to choose a number to decrement below zero

I'm learning js. I want to make an alarm clock app. Currently I'm working on setting the time. I created buttons that increment and decrement the hour. I want to decrement below zero to 23. I also want to stop the increment at 23 and then continue with 0. Can anyone help me? If someone tells me the first part of the condition, I'll be able to figure out the other part.

let myHour = document.querySelector(".hours");
let myHourConverted = Number(myHour.innerText);
const hourDecrementBtn = document.querySelector(".hour-decrement");
const hourIncrementBtn = document.querySelector(".hour-increment");

hourDecrementBtn.addEventListener("click", decrementHour);

function decrementHour() {
  let hourDecrement = --myHourConverted;
  myHour.innerText = hourDecrement;

  if (hourDecrement < 0) {
    hourDecrement = 23;
  }
}

hourIncrementBtn.addEventListener("click", incrementHour);

function incrementHour() {
  let hourIncrement = ++myHourConverted;
  myHour.innerText = hourIncrement;
  if (hourIncrement > 23) {
    hourIncrement = 0;
  }
}
<div class="timer-hours-container">
  <span class="hours">00</span>
  <div class="hours-buttons-container">
    <button class="hour-decrement timer-button">&lang;</button>
    <button class="hour-increment timer-button">&rang;</button>

almost 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The problem is that when you make the change to decrement or increment from 23 to 0, you change the content of hourIncrement but not of myHourConverted, I leave you a small solution.

const myHour = document.querySelector(".hours");
let myHourConverted = Number(myHour.innerText);

const hourDecrementBtn = document.querySelector(".hour-decrement");
const hourIncrementBtn = document.querySelector(".hour-increment");

function decrementHour() {
    if (--myHourConverted < 0) myHourConverted = 23;
    myHour.innerText = myHourConverted;
}

function incrementHour() {
    if (++myHourConverted > 23) myHourConverted = 0;
    myHour.innerText = myHourConverted;
}

hourDecrementBtn.addEventListener("click", decrementHour);
hourIncrementBtn.addEventListener("click", incrementHour);
<div class="timer-hours-container">
  <span class="hours">00</span>
  <div class="hours-buttons-container">
    <button class="hour-decrement timer-button">&lang;</button>
    <button class="hour-increment timer-button">&rang;</button>
  </div>
</div>
almost 4 years ago · Santiago Trujillo 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