Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

209
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!