# How do I convert a digit (430) into minutes/seconds (7:30)?

Show 200 post(s) from this thread on one page
Page 2 of 2 First 12
• May 20, 2011, 08:06
StevenHu
By using jslint repeatedly and researching to understand the errors, I've come to the following:

HTML Code:

```var document,     runtime = document.getElementById('runtime'),     resultPitStops = document.getElementById('resultPitStops');     function formFuel(form) {     var a = form.elements.tank.value,         b = form.elements.driveMinutes.value,         c = form.elements.driveSeconds.value,         d = form.elements.consumed.value,         used = a - d, // get the amount of fuel used  -->         secs = b * 60, // convert minutes into seconds  -->         secstotal = secs + c, // add minutes and seconds to get total seconds  -->         secsPerCC = secstotal / used, // divide the number of seconds into the used fuel amount for secs per cc  -->         seconds = secsPerCC % 60, // use modulus operator to get the remainder and convert into seconds figure  -->         minutes = math.floor(secsPerCC / 60),  // get the minute figure as a digit  -->         runtime2 = minutes + ":" + seconds; // concatenate everything into minutes and seconds  -->     form.elements.ans.value = runtime2; }   function heatpit(form) {     var e = form.elements.LENGTH.value, // put LENGTH field value into variable e  -->         f = form.elements.STOPS.value, // put STOPS field value into variable f  -->         secs2 = e * 60, // convert the minutes into seconds  -->         periods = f + '1', // add 1 to the pit stop number get the total  number of divisions throughout the period  -->         result2 = secs2 / periods, // divide the full time by the number of periods  -->         seconds = result2 % 60, // use modulus operator to get the remainder and convert into seconds figure  -->         minutes = math.floor(result2 / 60), // get the minute figure as a digit  -->         time = minutes + ":" + seconds; // concatenate everything into minutes and seconds  -->     form.elements.between.value = time; // display in the "between" field s/b "time" value  --> } runtime.onclick = function () { // https://developer.mozilla.org/en/DOM/element.onclick     var form = document.getElementById('form1');     formFuel(form); } resultPitStops.onclick = function () {     var form = document.getElementById('form2');     heatpit(form); }```
I still don't understand why a semi-colon is a problem inside the last two functions. When I remove them, then more errors appear. I also don't know how to 'define' math.

This has been a good learning experience.
• May 20, 2011, 08:08
StevenHu
The code still isn't working. Do you think defining 'math' is the problem?
• May 20, 2011, 11:06
Jay.P
'math' isn't defined, I think you should be using 'Math'.

And the following two lines should be ended with a semicolon
Quote:

Originally Posted by StevenHu

HTML Code:

```runtime.onclick = function () { // https://developer.mozilla.org/en/DOM/element.onclick     var form = document.getElementById('form1');     formFuel(form); } resultPitStops.onclick = function () {     var form = document.getElementById('form2');     heatpit(form); }```

• May 20, 2011, 11:08
StevenHu
Thanks! I did make those two changes, but the code still won't work.
• May 20, 2011, 11:23
Jay.P
What's your whole code like right now, including the form?
• May 20, 2011, 11:26
StevenHu
<script language="JavaScript">

function heatpit(form) {
var e = form.elements.LENGTH.value, // put LENGTH (number of minutes) field value into variable e
f = form.elements.STOPS.value, // put STOPS field value into variable f
secs2 = e * 60, // convert the minutes into seconds
periods = f + '1', // add 1 to the pit stop number get the total number of divisions throughout the LENGTH
result2 = secs2 / periods, // divide the LENGTH by the number of periods
seconds = result2 % 60, // use modulus operator to get the remainder and convert it into seconds figure
minutes = Math.floor(result2 / 60), // get the minute figure as a digit
time = minutes + ":" + seconds; // concatenate everything into minutes and seconds

form.elements.between.value = time; // display "time" value in the "between" field
}

var resultPitStops = document.getElementById('resultPitStops');
resultPitStops.onclick = function () {
var form = document.getElementById('form2');
heatpit(form);
};

</script>

</body>
</html>
[/HTML]
• May 20, 2011, 11:40
Jay.P
Ahh, y'know the moment when it hits you suddenly and you feel silly? I just got that and you might too :P

Here's a fixed heatpit():

HTML Code:

```function heatpit(form) {     var e = form.elements.LENGTH.value, // put LENGTH (number of minutes) field value into variable e         f = parseInt(form.elements.STOPS.value), // put STOPS field value into variable f         secs2 = e * 60, // convert the minutes into seconds         periods = f + 1, // add 1 to the pit stop number get the total  number of divisions throughout the LENGTH         result2 = secs2 / periods, // divide the LENGTH by the number of periods         seconds = result2 % 60, // use modulus operator to get the remainder and convert it into seconds figure         minutes = Math.floor(result2 / 60), // get the minute figure as a digit         time = minutes + ":" + seconds; // concatenate everything into minutes and seconds     form.elements.between.value = time; // display "time" value in the "between" field }```

The changes being:
f = parseInt(form.elements.STOPS.value)
and
periods = f + 1

see, the form field is a string, making f a string that just happened to contain an integer.
if you typed '1' into the form, it'd calculate:
periods = '1' + '1';
to get the result '11'!

parseInt() converts to to an integer, and removing the quotes around 1 stops it being converted back to append the '1' character :D

Try fixing formFuel in the same way!
• May 20, 2011, 11:45
StevenHu
Wow, at last it works! A hearty thank you!
• May 21, 2011, 22:08
paul_wilkins
Quote:

Originally Posted by Jay.P
see, the form field is a string, making f a string that just happened to contain an integer.
if you typed '1' into the form, it'd calculate:
periods = '1' + '1';
to get the result '11'!

parseInt() converts to to an integer, and removing the quotes around 1 stops it being converted back to append the '1' character :D

If this helps, it can be a good practice to always use Number(form.elements.fieldname.value) whenever you expect the value to be used as a number.

That way, if it's not a number you can provide a default for it instead:

Code javsacript:

`var stops = Number(form.elements.fieldname.value) || 0;`

And of course when it absolutely must be a whole number, use parseInt but don't forget to include the radix indicator too, otherwise entries of "08" or "09" won't end up as you expect them to be.

Code javascript:

`var stops = parseInt(form.elements.fieldname.value, 10) || 0;`
• May 23, 2011, 08:22
StevenHu
Wow, thanks for this valuable information!
• May 25, 2011, 18:19
paul_wilkins
Quote:

Originally Posted by techforums
Straight forward division using calculator will give you ...

And when performing division in binary

4278 / 60 is equivalent to
1000010110110 / 111100

Code:

```            01000111 <-- minutes       ______________ 111100 )1000010110110       -111100|||||||         ------v||||||         1000010||||||         -111100||||||         ------v|||||             1101|||||         -111100|||||           ------v||||             11011||||           -111100||||           ------v|||             110110|||           -111100|||             ------v||             1101101||             -111100||             ------v|             1100011|             -111100|               ------v               1001110               -111100               ------                 10010 <-- seconds```
The quotient is the number of minutes, and the remainder is the number of seconds.

01000111 makes for 79 minutes, and 10010 is 26 seconds.
But this has a similar lack of relevance as that with post #36
Show 200 post(s) from this thread on one page
Page 2 of 2 First 12