Just Doo
Just Doo

Reputation: 91

vanilla JS for-loop issue

Throughout for-loop in rosteringArray(), the array is should be like this.

array[0] = startDate (in form of milliseconds)

array[1] = startDate (in form of milliseconds) + 86400000

...

array[array.length-1] = endDate (in form of milliseconds)

But it doesn't.

how can I develop this code?

var rosteringArray = function(yy1, mm1, dd1, yy2, mm2, dd2){
  var date = new Date(yy1,mm1,dd1);
  var sDate = new Date(date);

  var timeStart = date.getTime() + 86400000*9; //

  var date = new Date(yy2,mm2,dd2);
  var sDate = new Date(date);

  var timeEnd = date.getTime() + 86400000*9;

  var end = timeStart;
  var start = timeEnd;
  
  var countDateBetween = (end - start)/86400000 +1;
  var array = new Array;
  for (var g = 0; g < countDateBetween; g++){
    for (var h =setStart; h < setEnd; h=h+86400000){
    array[g] = h;
    return array;
    }
  }
}

console.log(rosteringArray(2020,0,1,2020,0,03));

Upvotes: 0

Views: 80

Answers (1)

Nikolai
Nikolai

Reputation: 306

You've mixed propery values:

var end = timeStart; var start = timeEnd;

and you don't need second loop, which will always set last day value for all properties in array.

Take a look at the working snippet:

var rosteringArray = function(yy1, mm1, dd1, yy2, mm2, dd2){
  var date = new Date(yy1,mm1,dd1);
  var sDate = new Date(date);

  var timeStart = date.getTime() + 86400000*9; //

  var date = new Date(yy2,mm2,dd2);
  var sDate = new Date(date);

  var timeEnd = date.getTime() + 86400000*9;

  var end = timeEnd;
  var start = timeStart;
  console.log(start);
  console.log(end);
  
  var countDateBetween = (end - start)/86400000 +1;
  console.log(countDateBetween);
  var array = new Array;
  var h =start;
  for (var g = 0; g < countDateBetween; g++){
    
    array[g] = h;
    h=h+86400000;
    }
return array;
}

console.log(rosteringArray(2020,0,1,2020,0,03));

Upvotes: 1

Related Questions