1

I've got a selection of times, but I want to keep the leading zero:

var fastTrainReading = [0943, 0957, 1006, 1013 , 1027, 1036, 1043, 1057, 1106, 1113, 1127, 1136, 1213, 1227, 1236, 1243, 1257, 1306, 1313, 1327, 1336, 1343, 1357, 1406, 1413, 1427, 1436, 1443, 1457, 1506, 1513, 1527, 1537, 1543, 1559, 1606, 1613, 1627, 1636, 1643, 1657, 1704, 1718, 1728, 1735, 1749, 1758, 1816, 1830, 1847, 1859, 1906, 1911, 1930, 1936, 1941, 1959, 2006, 2017, 2027];

This is the math performed:

var currentTime = hour*100 + mins;
if ((day == 0) || (day == 6)) {
    document.write ("There are no buses today");
}  else {

var displayCount = 0;
        var TrainStr1 = "";
        for (var i=0, len=fastTrainReading.length; i<len; ++i) {
            if ((fastTrainReading[i] > currentTime) && (displayCount < 2)) {
                displayCount = displayCount+1;
                TrainStr1=TrainStr1 + fastTrainReading[i] + "<br/>";
            }
        }
    }
document.write (TrainStr1)

I had a pretty good search through, if I missed something feel free to abuse me (but point me in the right direction).

tshepang
  • 12,111
  • 21
  • 91
  • 136
Jack Tyler
  • 117
  • 1
  • 2
  • 5
  • Force the variables type to string: `TrainStr = "" + 0975;` – Michael May 24 '11 at 10:30
  • 1
    Actually, I think the leading zero indicates that a raw number is in Octal, but 0943 is not valid octal. The array should store plain integers (`943`) or strings (`'0943'`). – James McCormack May 24 '11 at 10:35
  • good point james. I just read them as strings because of the leading 0. The 0943 and 0957 is the only broken one in his set though. I'd normalize on strings for his specific question though. – John Green May 24 '11 at 10:43
  • I think, the input list shouldn't contain leading zeros because that's part of the display, not the logic. Add the leading zero @ TrainStr1 += paddingFunction(fastTrainReading[i]) + "
    ";
    – roberkules May 24 '11 at 10:48

5 Answers5

2

Simplest solution is to store your time data as strings e.g. var fastTrainReading = ['0943', .... JavaScript will cast to integer for you in your calculation routines.

For a comprehensive string formatting solution that adheres to conventional principles, try sprintf() for javascript: http://www.diveintojavascript.com/projects/javascript-sprintf

James McCormack
  • 9,217
  • 3
  • 47
  • 57
1

You can try to use .toString()
like:
TrainStr1=TrainStr1 +fastTrainReading[i].toString()+ "<br/>";
alt to save your times as strings.

Johan Olsson
  • 608
  • 3
  • 10
1

By default you won't get the leading zeroes.

As you know the length of TrainStr1 is 4, you can use the following function to get zeroes.

function formatted(time) {
    var s = "0000" + time;
    return s.substr(s.length-4); }

You can call the function 'formatted' before using document.write

Lucky Murari
  • 12,672
  • 5
  • 22
  • 43
0

You need to zero pad your numbers.

Number.prototype.zf = function _zeroFormat(digits)
{
    var n = this.toString(), pLen = digits - n.length;
    for ( var i = 0; i < pLen; i++)
    {
        n = '0' + n;
    }
    return n;
}

if ((fastTrainReading[i] > currentTime.zf(4)) && (displayCount < 2)) {
   displayCount = displayCount+1;
   TrainStr1=TrainStr1 + fastTrainReading[i] + "<br/>";
}

Once you've normalized all of your numbers to be 0-padded to 4 digits, string comparison is possible. Otherwise, you'll have issues. As things stand, it looks like your code was trying to compare a string (like an element from fastTrainReading) and a number (currentTime).

John Green
  • 13,241
  • 3
  • 29
  • 51
0

Just declare your array as strings:

var fastTrainReading = ['0943', '0957', '1006', '1013'];

And don't worry fastTrainReading[i] > currentTime will still work. '100' > 99 == true

herostwist
  • 3,778
  • 1
  • 26
  • 34