user5304564
user5304564

Reputation:

How do I get the next entry in an array instead of the current one?

I have a snippet of code that does this:

If it's between 0000 and 0600, it'll display "Night." If it's between 0600 and 1200, it'll display "Morning." If it's between 1200 and 1800 it shows "Afternoon" and between 1800 and 2400 "Evening."

What I want it to do, however, is to show the next entry in the array, not the current one. So, between 0600 and 1200 it should show "Afternoon" and not "Morning."

Here is my code for it:

function startTime() {

  var today = new Date();
  var h = today.getHours();
  var m = today.getMinutes();
  var wv = today.getDay();

  var hv = 100 * h + m;

  var wv1 = new Array();
  wv1[0] = '0000,0600,Night';
  wv1[1] = '0600,1200,Morning';
  wv1[2] = '1200,1800,Afternoon';
  wv1[3] = '1800,2400,Evening';
  
  if (wv == 1) {
    wv1.forEach(function(entry) {
      var schedule = entry.split(',');
      if (hv >= schedule[0] && hv <= schedule[1]) {

        var timeofday = schedule[2];

        //Schedule
        document.getElementById('day').innerHTML = timeofday;

      }
    });
  }
}
<body onload="startTime()">
  <div id="main-wrapper">
    <div id="day"></div>
  </div>
</body>

Upvotes: 0

Views: 106

Answers (6)

bencodezen
bencodezen

Reputation: 1063

To CodeOcelot's point, the way you are approaching is more complicated than necessary, but I'm going to answer this assuming you need to follow this specific model.

Solution: I would add the array parameter to access the current array in conjunction with the index to access the next item.

  if (wv == 1) {
    wv1.forEach(function(entry, index, array) {
      var schedule = entry.split(',');
      var nextItem = array[index++].split(',');

      if (hv >= schedule[0] && hv <= schedule[1]) {
        var nextTime = wv1[index >= wv1.length ? 0 : ++index];
        var timeofday = nextItem[2];

        //Schedule
        document.getElementById('day').innerHTML = timeofday;
      }
    });
  }
}

Hope that this helps!

Upvotes: 0

Raxa
Raxa

Reputation: 382

You wrote code like someone before hundred years (just kidding). Please be smart and short. (BTW: I like the simple if-statements from CodeOcelot).

Another more declarative approach can be this (and it is without the if-statement that makes every code more complex). This can be done since the names are redundant to the clock-numbers.

var names = ["Night","Morning","Afternoon","Evening"]
var index = Math.floor(new Date().getHours() * names.length / 24)
document.getElementById('day').innerHTML = names[index];

Upvotes: 0

Moishe Lipsker
Moishe Lipsker

Reputation: 3032

Just add a check to see if the next index is more than the length. If it is, use 0. Otherwise use the next index.

function startTime() {

  var today = new Date();
  var h = today.getHours();
  var m = today.getMinutes();
  var wv = today.getDay();

  var hv = 100 * h + m;

  var wv1 = new Array();
  wv1[0] = '0000,0600,Night';
  wv1[1] = '0600,1200,Morning';
  wv1[2] = '1200,1800,Afternoon';
  wv1[3] = '1800,2400,Evening';
  
  if (wv == 1) {
    wv1.forEach(function(entry, index) {
      var schedule = entry.split(',');
      if (hv >= schedule[0] && hv <= schedule[1]) {
        var nextTime = wv1[index >= wv1.length - 1 ? 0 : ++index];
        var timeofday = nextTime.split(',')[2];

        //Schedule
        document.getElementById('day').innerHTML = timeofday;

      }
    });
  }
}
<body onload="startTime()">
  <div id="main-wrapper">
    <div id="day"></div>
  </div>
</body>

Upvotes: 0

blex
blex

Reputation: 25634

I would do it slightly differently:

startTime();

function startTime(){
    var hours = new Date().getHours();
    var i = Math.floor(hours/6);
    var timeofday = ['Morning','Afternoon','Evening','Night'][i];
    document.getElementById('day').innerHTML = timeofday;
}
<div id="day"></div>

Demo with every hour

function startTime(hours){
    var i = Math.floor(hours/6);
    var timeofday = ['Morning','Afternoon','Evening','Night'][i];
    return timeofday;
}

for(var h=0; h<24; h++){
    document.body.innerHTML += h +'h => '+ startTime(h) +'<br>';
}

Upvotes: 3

Me.Name
Me.Name

Reputation: 12544

The easiest way to get the next block in your current setup, would be to simply add 600 to the current time:

 var hv = (600 + 100 * h + m) % 2400;

Of course with other setups/objects, other solutions are possible, but getting the next block can still be done this way without an extra call for the 'next' index

Upvotes: 0

CodeOcelot
CodeOcelot

Reputation: 3513

I'm not sure why your code is unnecessarily complicated. Why not use something like:

var today = new Date(); 
var message;
if(today.getHours() <= 6) message = "Morning"
// etc

Upvotes: 1

Related Questions