1

I am making a simple program that determines what time the next train is. I have an array with all the train times, I just don't know how to determine which is the closest to the current time, without going over. Here is what I have so far:

var d = new Date();
var hours = d.getHours();
var minutes = d.getMinutes();

var time = hours + minutes;
console.log(time);

var trainTimes = [1125, 1155, 1225, 1255, 1325, 1355, 1425, 1455, 1526, 1629, 1644, 1709];

function nextTrain(time) {

}

Within the function nextTrain() should I simply do if-else statements, or is there a better method?

6 Answers6

5

Since the array of trainTimes is in order, you could loop through them until you find the first one after the current time, then break.

function nextTrain(time) {
  for (i = 0; i<trainTimes.length; i++){
    if(trainTimes[i]>time){
     console.log("the next train is " + trainTimes[i]);
     break;
    }
  }
}
Mark Anderson
  • 663
  • 1
  • 8
  • 14
2
var d = new Date();
var hours = d.getHours();
var minutes = d.getMinutes();

var time = hours + minutes;
console.log(time);

// given sorted array.
var trainTimes = [1125, 1155, 1225, 1255, 1325, 1355, 1425, 1455, 1526, 1629, 1644, 1709];

// returns nth train 
function nextTrain(num, arr) {
    n = 0
    while (arr[n] <= num){ // consider < vs. <=
      n++;
    }
    return arr[n];
}

console.log(nextTrain(time,trainTimes))

idea taken from link

Community
  • 1
  • 1
taesu
  • 4,482
  • 4
  • 23
  • 41
0

I would suggest you to do binary search here since your array is already sorted. You can check this link for detail regarding implementation.

After you figure out, where the current time falls in the array, you can calculate the difference of current time with two closest neighbors. This would depend on where your current time falls in the binary tree. Anyway, this method will be efficient.

Community
  • 1
  • 1
user109260
  • 878
  • 7
  • 22
0

You need to multiply your hours by 100.

You could then try something like this:

nextTrain(time);

function nextTrain(time) {
    var timeFound = false;
    for(var i = 0; i < trainTimes.length; i++){
        if(time > trainTimes[i - 1] && time < trainTimes[i] && !timeFound ){
            console.log("Next train time is: " + trainTimes[i]);
            timeFound=true;
        }
    }
}
SirJackovich
  • 71
  • 1
  • 9
0

i hope it helps you

var d = new Date();
var hours = d.getHours();
var minutes = d.getMinutes();

var time = hours + minutes;
console.log(time);

var trainTimes = [1125, 1155, 1225, 1255, 1325, 1355, 1425, 1455, 1526, 1629, 1644, 1709];
var diff = new Array(12);
for (i = 0; i < trainTimes.length; i++) {
    diff[i] =Math.abs( time + trainTimes[i]);//difference
}
var diff_sort = diff.sort(function(a, b){return a-b});//acs sort
var nearestIndex = diff.indexOf(diff_sort[0]); 
var nearestValue = trainTimes[nearestIndex]; 
alert(nearestValue)
Mam Ghagh
  • 33
  • 4
0

Just use this:

var nextTimeIndex = (trainsTimes.indexOf(time) >= trainsTimes.length) ? 0 : trainsTimes.indexOf(time) + 1;
var nextTrain = trainsTimes[nextTimeIndex];
Sherali Turdiyev
  • 1,745
  • 16
  • 29