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

203
Visualizações
how to divide the time into periods each specific variable in JavaScript

what im trying to do is that i have a branch that got workhours and so there is more than one services this branch give , so if the client press on service that its duration 15 minutes i need to divide the work hours of the branch into queue list of this duration , from open shift till endshift and also delete all the queues that are on breaktime of the branch

i have this variables:

Startshift="09:10" , breaktime = "12:00" , endBreak="12:30" , endShift="13:00" , period each :10min

i want to define an array of time periods that contain in each cell specific hour according to specific variable i give

myCode:

let startTime = 9,
    endTime = 13,
    selectedServiceDuration = 10,
    middleMin = 10;

for (i = startTime; i < endTime; i++) {

    for (j = middleMin; j < 60; j = j + selectedServiceDuration) {

        arr.push({
            id: Math.random() * 56,
            hour: i + ":" + (j === 0 ? "00" : j)
        });
    }
}

it will return this way

"hour": "9:10",
"hour": "9:20",
"hour": "9:30",
"hour": "9:40",
"hour": "9:50",
"hour": "10:10",
"hour": "10:20",
"hour": "10:30",
"hour": "10:40",
"hour": "10:50",
"hour": "11:10",
"hour": "11:20",
"hour": "11:30",
"hour": "11:40",
"hour": "11:50",
"hour": "12:10",
"hour": "12:20",
"hour": "12:30",
"hour": "12:40",
"hour": "12:50",

so my code missed hour : 10:00 so that mean the rest of the tabe is incorrect

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

0

Your loop start from 10 and terminate while j = 50 because your condition is j < 60. You need to increment loop till 60 so your condition become j <= 60. You check j === 0 but loop start from 10 so how it's going to 0. Better way is check hour with j === 60.

Here below is modified code:

const arr = [];
for(i=9; i<13; i++) {

    for(j=10; j<=60; j=j+10) {
        arr.push({id:Math.random()*56 , hour:(j === 60 ? (i + 1) : i) + ":" + (j === 60 ? "00" : j)} );
    }
}

for (var k = 0; k <= arr.length - 1; k++)
{
    console.log(arr[k].hour);
}

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