Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

206
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda