javascript - Dividing a number to equal chunks -


i'm trying write function returns array of equally chunked dates , number of days pertaining dates. should there remainder of days appended array follow.

expected outcome:

[{ 'startdate' : 20160719 //dates generated momentjs 'numofdays': 5 },{ 'startdate' : 20160724 'numofdays': 5 },{ 'startdate' : 20160729 'numofdays': 3 }] 

below function i've written in can pass in start date (momentjs), total number of days (daystodisplay) , number of days divided (numofdays).

function buildqueue(startdate, numofdays, daystodisplay) {     if (!startdate || !numofdays || !daystodisplay) {         throw new error('all params required!');     }      var num = numofdays > daystodisplay ? daystodisplay : numofdays;     var div = math.floor(daystodisplay / num);     var count = daystodisplay;     var rem = daystodisplay % num;     var lastitem;     var i;     var arr = [];      (i = 0; <= daystodisplay; += num) {          arr.push({           startdate: moment(startdate, 'yyyymmdd').add(i, 'days').format('yyyymmdd'),           numofdays: numofdays,           count:         })          if (rem === count) {             break;         }          count -= num;      }      if (count > 0) {         lastitem = arr[arr.length - 1];         var leftover = daystodisplay - lastitem.count;         arr.push({             startdate: moment(lastitem.startdate, 'yyyymmdd').add(num, 'days').format('yyyymmdd'),             numofdays: rem,             count: leftover + lastitem.count         });     }      return arr;  } 

a working example here (https://jsfiddle.net/zv5ghqpa/1/). code appears work in scenarios daystodisplay dividable more 2.

when daystodisplay dividable one, additional item in returned array due 0 index in loop. expected outcome if call buildqueue('20160719', 5, 8) should be:

[{     'startdate': 20160719     'numofdays': 5 }, {     'startdate': 20160724     'numofdays': 3     }] 

instead returning:

[{     'startdate': 20160719     'numofdays': 5 },{     'startdate': 20160724     'numofdays': 5 }, {     'startdate': 20160729     'numofdays': 3     }] 

i hope i've given enough info... doing head in.

thanks in advance!

i think code you're looking for:

function buildqueue(startdate, numofdays, daystodisplay) {    if (!startdate || !numofdays || !daystodisplay) {     throw new error('all params required!');   }    var num = numofdays > daystodisplay ? daystodisplay : numofdays;   var div = math.floor(daystodisplay / num);   var count = daystodisplay;   var rem = daystodisplay % num;   var n = 0;   var i;    var arr = [];    (i = 0; <= daystodisplay; += num) {      arr.push({       startdate: moment(startdate, 'yyyymmdd').add(i, 'days').format('yyyymmdd'),       numofdays: daystodisplay % num,       count:     })      console.log(rem + ' ' + count);      if (rem === count) {       count = 0;       break;     }      count -= num;    }    if (count > 0) {     var leftover = daystodisplay - arr[arr.length - 1].count;     arr.push({       startdate: moment(arr[arr.length - 1].startdate, 'yyyymmdd').add(num, 'days').format('yyyymmdd'),       numofdays: daystodisplay % num,       count: leftover + arr[arr.length - 1].count     });   }    return arr;  }  //console.log(buildqueue(moment(), 80, 100)); console.log(buildqueue(moment(), 5, 8)); //console.log(buildqueue(moment(), 15, 100)); //console.log(buildqueue(moment(), 30, 100)); //console.log(buildqueue(moment(), 45, 100)); 

Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -