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

208
Visualizações
How should I convert the output from minutes to an HH:MM format?

The code is simple, I'm very new to programming. You put in the points you need and the result of the game and the output is the minutes of gameplay needed for the points.

It works, my question is how should I convert the output to be in an HH:MM format? I tried if/else cycles, but it's a lot of work, not to mention it's very primitive and hardcoded. Are for cycles the answer? Should I start from the beginning altogether?

Thank you in advance!

function passPoints(input) {
    let gameResult = String(input[0]);
    let pointsNeeded = Number(input[1]);

    let pointsPerMinute = 0;

    if (gameResult == 'w') {
        pointsPerMinute = 6;
    } else if (gameResult == 'l') {
        pointsPerMinute = 4;
    }

    let minutesOfGameplayNeeded = pointsNeeded / pointsPerMinute;


    console.log(minutesOfGameplayNeeded)
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Here's how I would approach it. Using modulus and dividing by 60 to get the h:m

// as a helper function
const getTimeReadout = m => {
  let a = [Math.floor(m / 60), Math.floor(m % 60)]
  return a.map(t => ('0' + t).slice(-2)).join(':')
}

Here it is integrated into your code:

function passPoints(input) {
  let gameResult = String(input[0]);
  let pointsNeeded = Number(input[1]);

  let pointsPerMinute = 0;

  if (gameResult == 'w') {
    pointsPerMinute = 6;
  } else if (gameResult == 'l') {
    pointsPerMinute = 4;
  }

  let m = pointsNeeded / pointsPerMinute
  let minutesOfGameplayNeeded = [Math.floor(m / 60), Math.floor(m % 60)];
  // now it's in an array so we can iterate it below (adding the zero if needed)
  minutesOfGameplayNeeded = minutesOfGameplayNeeded.map(t => ('0' + t).slice(-2)).join(':')

  console.log(minutesOfGameplayNeeded)
}

passPoints(['w', 427]);

Same thing, but I optimized your code a bit:

function passPoints(input) {
  [gameResult, pointsNeeded] = input
  let pointsPerMinute = gameResult == 'w' ? 6 : gameResult == 'l' ? 4 : 0;
  let m = pointsNeeded / +pointsPerMinute;
  return [Math.floor(m / 60), Math.floor(m % 60)].map(t => ('0' + t).slice(-2)).join(':')
}
console.log(passPoints(['w', 427]));

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