Forum Moderators: open
result = hour + ":" + minute
return result
Which works fine if (minute >= 10). But how do I get a leading zero if (minute < 10)?
In case I am not totally clear let me give an example. For 3.25 hours I get 3:15, while for 3.1 hours I get 3:6 instead of 3:06 :(
TIA!
You might have to formally declare your variable as a string first, so it doesn't just add the two integers together. You might have to use a "toString()" method, but try the below first.
I think this will work:
if (minute < 10) {
minute = "0" + minute;
}
result = hours + ":" + minute;
This was my first JavaScript program, and though I do not see myself using it much, you never know!
confusing
...like when you need to nest them four or five levels ;) Like when I had to make my own leap year check. Try expressing this in one line, using ternary functions:
"The year is a leap year if modulus 4 is zero, unless modulus 100 is also zero, unless modulus 400 is zero as well" :)
Hence, 1900 is not leap year, 1976 is, and so is 2000...
a = 4; // a is now an integer
a += ""; // a is now a string
a = Math.round(a); //a is now an integer again
There are functions, but basically... if you're using a string function, it will become a string. If you're using an int function, it will become an int.
Now, certain care has to be taken. This would not work!
a = 400;
b = substring(a,0);
However, this would:
a = 400;
b = substring(a+"",0);
As DrDoc points out Javascript is a heavily untyped language, with generous automatic value conversion.
Generally speaking, any value used in the context of another will convert to fit the context. Hence "0"+minute (integer concatenated to a string) results in a string.
//
I've tested this a bit, but I don't have a long list of leap years and that's the damnedest thing to find on the web!?
var year = 1201;
document.write(year%4==0&&year%100!=0?"Leap":(year%400!=0?"No Leap":"Leap"));
If it performs to your satisfaction may I have a prize please. I'd like a prize. I need a prize.
T