0

Hey all this is the array I have so far:

var thisDayDate = today.getDate();
var JANDays = [{day:16, timeStart:"23:00:00", timeEnd:"23:59:59"},  //It's Jan 16th Friday 11pm to
               {day:17, timeStart:"00:00:00", timeEnd:"11:00:00"},  //It's Jan 17th                Sat 11am
               {day:22, timeStart:"23:00:00", timeEnd:"23:59:59"},  //It's Jan 22nd Friday 11pm to  
               {day:23, timeStart:"00:00:00", timeEnd:"07:00:00"}]; //It's Jan 23ed                Sat  7am

for(var i = 0; i < JANDays.length; i++)
{
    if (thisDayDate == [JANDays[i].day]) {
        if (isSiteDown(JANDays[i].timeStart,JANDays[i].timeEnd)) { 
           showThePage(); 
        } else { 
           console.log('nope');
        }
    }
}

My issue is that I am only wanting to do this for the array:

var JANDays = [{day:16, timeStart:"23:00:00"},  //It's Jan 16th Friday 11pm to
               {day:17, timeEnd:"11:00:00"},    //It's Jan 17th                Sat 11am
               {day:22, timeStart:"23:00:00"},  //It's Jan 22nd Friday 11pm to  
               {day:23, timeEnd:"07:00:00"}];   //It's Jan 23ed                Sat  7am

In the above code I am wanting just to have a timeStart for the first day and an timeEnd for the second day etc etc.

The timeEnd for the 1st day should be static at 23:59:59

The timeStart for the 2nd day should be static at 00:00:00

I am not sure how to go about doing that in my IF statement loop so help would be appreciated.

4
  • 2
    The first error I see is that 24:59:59 is not a valid time, then maybe I am the only one but I really don´t undesrtand what you need. Commented Dec 15, 2015 at 17:28
  • Could you explain a bit more? I though I was the only one who didn't get it, but seems like we are at least 2. Commented Dec 15, 2015 at 17:31
  • @bto thanks for catching that. Fixed in OP. Commented Dec 15, 2015 at 17:34
  • @Meeseeks understands my issue. Check his example. Commented Dec 15, 2015 at 17:53

3 Answers 3

1

If I understand correctly ... You want to use the following array:

var JANDays = [{day:16, timeStart:"23:00:00"},
               {day:17, timeEnd:"11:00:00"},
               {day:22, timeStart:"23:00:00"},
               {day:23, timeEnd:"07:00:00"}];

instead of the one you currently have.

In that case, you can increment the index i to get the next item in the array, for example:

var JANDays = [{day:16, timeStart:"23:00:00"},
               {day:17, timeEnd:"11:00:00"},
               {day:22, timeStart:"23:00:00"},
               {day:23, timeEnd:"07:00:00"}];

for (var i = 0; i < JANDays.length; i++)
{

    if (thisDayDate == JANDays[i].day) {

        // Notice "JANDays[i+1].timeEnd", this will get the timeEnd of the next item in the array
        // (Beware: i+1 could result in undefined)
        if (isSiteDown(JANDays[i].timeStart, JANDays[i+1].timeEnd)) { 
            showThePage(); 
        } else { 
            console.log('nope');
        }
    }
}

EDIT


I still don't fully understand what you're looking for but maybe look at this for a sec:

var JANDays = [{day:16, timeStart:"23:00:00"},
            {day:17, timeEnd:"11:00:00"},
            {day:22, timeStart:"23:00:00"},
            {day:23, timeEnd:"07:00:00"}];

for (var i = 0; i < JANDays.length; i++)
{
    var day = JANDays[i].day;
    var timeStart = JANDays[i] && JANDays[i].timeStart || "00:00:00";
    var timeEnd = JANDays[i] && JANDays[i].timeEnd;
    var nextTimeEnd = JANDays[i+1] && JANDays[i+1].timeEnd || "23:59:59";
    if (timeEnd) { // This indicates the second day
        console.log(day, timeStart, timeEnd);
    } else {
        console.log(day, timeStart, nextTimeEnd);
    }
}

The above will output to the console something like:

16 "23:00:00" "11:00:00"
17 "00:00:00" "11:00:00"
22 "23:00:00" "07:00:00"
23 "00:00:00" "07:00:00"

Each line consists of:

  1. The day, followed by;
  2. The timeStart, followed by;
  3. The timeEnd

So if the day is 16, then the timeStart and timeEnd will be "23:00:00" and "11:00:00", respectively.

But if the day is 17, then the timeStart and timeEnd will be "00:00:00" and "11:00:00", respectively.

Etc, etc.

The idea here is that I am checking whether timeEnd is defined during the loop.

Maybe this will help you?


EDIT


If this is what the output should look like:

16 "23:00:00" "23:59:59"
17 "00:00:00" "11:00:00"
22 "23:00:00" "23:59:59"
23 "00:00:00" "07:00:00"

then you can go:

for (var i = 0; i < JANDays.length; i++)
{
    var day = JANDays[i].day;
    // The code below will check to see if timeStart and timeEnd are defined
    // If they are defined then they will be used,
    // otherwise use the specified value after the pipes (||)
    var timeStart = (JANDays[i] && JANDays[i].timeStart) || "00:00:00";
    var timeEnd = (JANDays[i] && JANDays[i].timeEnd) || "23:59:59";
    if (thisDayDate == day) {
        console.log(day, timeStart, timeEnd);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

What would happen if it was day 17? Would it not get the timeStart for day 22 as the timeEnd?
Yes it would and I'm sure that's not what you're looking for. What do you expect to happen if it was day 17?
I'm wanting to hard code the timeEnd for the first day and hard code the timeStart for the second day. Then it starts over with the second pair of days, etc etc.
0

You can use map and then check the index to see it if's odd or even. Something like this:

var JANDays = [{
    day: 16,
    timeStart: "23:00:00",
    timeEnd: "24:59:59"
  }, //It's Jan 16th Friday 11pm to
  {
    day: 17,
    timeStart: "00:00:00",
    timeEnd: "11:00:00"
  }, //It's Jan 17th                Sat 11am
  {
    day: 22,
    timeStart: "23:00:00",
    timeEnd: "24:59:59"
  }, //It's Jan 22nd Friday 11pm to  
  {
    day: 23,
    timeStart: "00:00:00",
    timeEnd: "07:00:00"
  }
]; //It's Jan 23ed

var alt = JANDays.map(function(item,idx) {
    if (idx % 2) {
        return { day: item.day, timeStart: item.timeStart };
    }
    else {
        return { day: item.day, timeEnd: item.timeEnd };
    }
});

document.getElementById("output").innerHTML = JSON.stringify(alt);
<div id="output"></div>

Comments

0

Try this:

var JANDays = [{day:16, timeStart:"23:00:00", timeEnd:"24:59:59"},  //It's Jan 16th Friday 11pm to
               {day:17, timeStart:"00:00:00", timeEnd:"11:00:00"},  //It's Jan 17th                Sat 11am
               {day:22, timeStart:"23:00:00", timeEnd:"24:59:59"},  //It's Jan 22nd Friday 11pm to  
               {day:23, timeStart:"00:00:00", timeEnd:"07:00:00"}]; //It's Jan 23ed                Sat  7am

var output = JANDays.map(function(item, index){
  var temp = {};
  temp.day = item.day;
  
  if(index%2 == 0){
    temp.timeStart = item.timeStart;
  }
  else{
    temp.timeEnd = item.timeEnd;
  }
  return temp;
})

console.log(output);

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.