JokerMartini
JokerMartini

Reputation: 6147

Divide total minutes into even chunks

I'm trying to divide my total number of minutes into chunks. Which I get as far as taking the total number of minutes and divide it by the number of chucks.

For example if i have 8 hours and i want to divide that into 3 even chunks. It returns 2.67. However since time loops on the 60, i would like my value to be 2.40

Which 2 hours and 40 minutes times 3 equals 8.0 hours. What am i doing wrong here?

function divideTimeByChunks(hours=0, minutes=0, chunks=0) {
    var totalMinutes = (hours * 60.0) + minutes;
    var minutesPerChunk = totalMinutes / chunks;
    var result = (minutesPerChunk / 60.0).toFixed(2);
    return result
}

var hours = parseInt($("#hours-input").val());
var minutes = parseInt($("#minutes-input").val());
var chunks = parseInt($("#chunks-input").val());
var result = divideTimeByChunks(hours, minutes, chunks);
$("#output").val(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div>
<label for="hours-input">Hours</label>
<input id="hours-input" type="text" value="8" maxlength="5">
<label for="minutes-input">Minutes</label>
<input id="minutes-input" type="text" value="0" maxlength="5">
</div>
<br>
<div>
<label for="chunks-input">Chunks</label>
<input id="chunks-input" type="text" value="3" maxlength="5">
</div>
<br>
<div>
<label for="output">Output</label>
<input id="output" type="text" readOnly>
</div>

Upvotes: 1

Views: 292

Answers (3)

Anurag Srivastava
Anurag Srivastava

Reputation: 14413

Calculate everything in terms of seconds:

function pad(val) { return val < 10 ? `0${val}` : val; }

function divideTimeByChunks(hours = 0, minutes = 0, chunks = 0) {
  var totalSeconds = hours * 3600 + minutes * 60;
  var secondsPerChunk = totalSeconds / chunks;
  var hrs = Math.floor(secondsPerChunk / 3600);
  var mins = Math.floor((secondsPerChunk % 3600) / 60);
  //console.log(secondsPerChunk, hrs, mins)
  var result = `${hrs}.${pad(mins)}`;
  return result;
}

function getChunks() {
  var hours = parseInt($("#hours-input").val());
  var minutes = parseInt($("#minutes-input").val());
  var chunks = parseInt($("#chunks-input").val());
  var result = divideTimeByChunks(hours, minutes, chunks);
  $("#output").val(result);
}

$('button').click(getChunks);
getChunks();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div>
  <label for="hours-input">Hours</label>
  <input id="hours-input" type="text" value="8" maxlength="5">
  <label for="minutes-input">Minutes</label>
  <input id="minutes-input" type="text" value="0" maxlength="5">
</div>
<br>
<div>
  <label for="chunks-input">Chunks</label>
  <input id="chunks-input" type="text" value="3" maxlength="5">
</div>
<br>
<div>
  <label for="output">Output</label>
  <input id="output" type="text" readOnly>
</div>
<br>
<button>Button</button>

Upvotes: 1

Lukas
Lukas

Reputation: 49

function divideTimeByChunks(hours=0, minutes=0, chunks=0) {
    var totalMinutes = (hours * 60.0) + minutes;
    var minutesPerChunk = totalMinutes / chunks;


    // We calculate the hours by just rounding down / using floor
    // Then we calculate the minutes by using % (moulo) to get the remaining minutes!

    var hours = Math.floor(minutesPerChunk / 60.0);
    var minutes = minutesPerChunk % 60.0;

    // Here you have the amount of hours and the amount of remaining minutes
    // in two variables.


    // If you want to have it the way you had it, use this!

    var result = (hours + minutes / 100).toFixed(2);

    return result
}

Upvotes: 1

kooskoos
kooskoos

Reputation: 4859

You are getting the math behind it wrong.

The 2.67 is the correct answer for hours

2.67 means two 100% = 2 hours + 67% of 1 hour = 40 mins

Since 100% is not 100mins but 60 mins you are getting confused.

I suggest first converting hours to minutes and then dividing in into chunks

(8*60)/3=160 mins = 2 hours 40 mins

If you want more precision go for seconds.

Upvotes: 1

Related Questions