3

I am having an issue figuring this out. The idea is that I have an array of values that I'm considering times, and I need to remove all values that overlap with a specific time:

var availableTimes = ['0 - 0.5', '0.5 - 1', '1 - 1.5', '1.5 -2', '2 - 2.5', '2.5 -3']

var timeToRemove = '0.5 - 2'

I have full control over what the array looks like, but that's the idea. I can't figure out how to construct my array of available times and my timeToRemove so that I can accomplish this. I am using a 24:00 hour clock, so 1 - 1.5 is 1:00 AM to 1:30 AM.

Any thoughts on the best way to construct this?

2
  • 1
    you'll have to loop on all items and then parse the strings into workable numbers and then check with some logic which returns true or false. I suggest looping the array using filter for that - developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Jun 5, 2014 at 17:11
  • Show us what the expected result should be after removing the times ? Commented Jun 5, 2014 at 17:21

3 Answers 3

2

I would prefer a OO way to do it:

(1) Construct an array with objects with following structure:

  {
        StartTime:[fload],
        EndTime:[fload]
  }

So your time array looks like this

 var availableTimes=
    [
      {StartTime:0,EndTime:0.5},
      {...}
    ];

(2) The time to be removed has the same structure:

  var timeToRemove = '0.5 - 2';
  =>
  var timeToRemove={StartTime:0.5,EndTime:2};

(3) Delete algorithm looks like this:

  for (var i=0;i<availableTimes.length;i++)
    {
      if(availableTimes[i].StartTime > timeToRemove.StartTime 
        && availableTimes[i].EndTime < timeToRemove.EndTime)
        availableTimes[i]=null; //perform deletion
    }
Sign up to request clarification or add additional context in comments.

Comments

1
   var availableTimes = ['0 - 0.5', '0.5 - 1', '1 - 1.5', '1.5 -2', '2 - 2.5', '2.5 -3']
   var timeToRemove = '0.5 - 2';

   var tempar = timeToRemove.replace(/\s/g,"").split('-');

   for(var i = 0, t = availableTimes.length; i < t; i++){
      var itemar = availableTimes[i].replace(/\s/g,"").split('-');
      if(tempar[0] === itemar[0] && tempar[1] === itemar[1]){
           availableTimes .splice(i, 1);
           return;
      }
   }

Comments

1

Since you have control over your data structures, I think you're better off structuring it as arrays of length 2 than strings (though you can convert what you have to this form by using split(" - "))

var availableTimes = [[0,0.5], [0.5,1], [1,1.5], [1.5 -2], [2,2.5], [2.5 -3]]
var timeToRemove = [0.5,2]

for (var i = 0; i<availableTimes.length;i++){
  //use >= and <= if you want a closed interval
  if ((availableTimes[i][0] > timeToRemove[0] &&
       availableTimes[i][0] < timeToRemove[1]) ||
      (availableTimes[i][1] > timeToRemove[0] &&
       availableTimes[i][1] < timeToRemove[1]))
     {
        console.log("overlapping time: " + availableTimes[i]);
     }
}

produces:

overlapping time: 0.5,1
overlapping time: 1,1.5 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.