mcclosa mcclosa - 2 months ago 13
Javascript Question

Increment variable every 5 iteration of for loop?

I'm not 100% sure how to phrase this question, so feel free to change the Question title to something that makes sense.

I have an object

solution
which contains a property name
days
which holds 10 arrays, see an example below

{
"sameShiftHolds": true,
"sameStaffHolds": true,
"sameRoomHolds": true,
"days": [{
"availableStaff": [
[0, 1, 4, 3, 5, 9, 22, 44],
[0, 1, 4, 3, 5, 9, 22, 44],
[4, 8, 7]
],
"availableRooms": [
[3, 6, 77, 89, 23],
[3, 6, 77, 89, 23],
[2, 7, 9]
],
"suitableStaff": [
[22, 44],
[22, 44],
[4]
],
"suitableRooms": [
[89, 23],
[22, 44],
[2]
],
"ValidStartDate": true
}, {
"availableStaff": [
[0, 1, 4, 3, 5, 9, 22, 44],
[0, 1, 4, 3, 5, 9, 22, 44],
[4, 8, 7]
],
"availableRooms": [
[3, 6, 77, 89, 23],
[3, 6, 77, 89, 23],
[2, 7, 9]
],
"suitableStaff": [
[22, 44],
[22, 44],
[4]
],
"suitableRooms": [
[89, 23],
[22, 44],
[2]
],
"ValidStartDate": false
}, {
"availableStaff": [
[0, 1, 4, 3, 5, 9, 22, 44],
[0, 1, 4, 3, 5, 9, 22, 44],
[4, 8, 7]
],
"availableRooms": [
[3, 6, 77, 89, 23],
[3, 6, 77, 89, 23],
[2, 7, 9]
],
"suitableStaff": [
[22, 44],
[22, 44],
[4]
],
"suitableRooms": [
[89, 23],
[22, 44],
[2]
],
"ValidStartDate": true
}, {
"availableStaff": [
[0, 1, 4, 3, 5, 9, 22, 44],
[0, 1, 4, 3, 5, 9, 22, 44],
[4, 8, 7]
],
"availableRooms": [
[3, 6, 77, 89, 23],
[3, 6, 77, 89, 23],
[2, 7, 9]
],
"suitableStaff": [
[22, 44],
[22, 44],
[4]
],
"suitableRooms": [
[89, 23],
[22, 44],
[2]
],
"ValidStartDate": true
}, {
"availableStaff": [
[0, 1, 4, 3, 5, 9, 22, 44],
[0, 1, 4, 3, 5, 9, 22, 44],
[4, 8, 7]
],
"availableRooms": [
[3, 6, 77, 89, 23],
[3, 6, 77, 89, 23],
[2, 7, 9]
],
"suitableStaff": [
[22, 44],
[22, 44],
[4]
],
"suitableRooms": [
[89, 23],
[22, 44],
[2]
],
"ValidStartDate": true
}, {
"availableStaff": [
[0, 1, 4, 3, 5, 9, 22, 44],
[0, 1, 4, 3, 5, 9, 22, 44],
[4, 8, 7]
],
"availableRooms": [
[3, 6, 77, 89, 23],
[3, 6, 77, 89, 23],
[2, 7, 9]
],
"suitableStaff": [
[22, 44],
[22, 44],
[4]
],
"suitableRooms": [
[89, 23],
[22, 44],
[2]
],
"ValidStartDate": true
}, {
"availableStaff": [
[0, 1, 4, 3, 5, 9, 22, 44],
[0, 1, 4, 3, 5, 9, 22, 44],
[4, 8, 7]
],
"availableRooms": [
[3, 6, 77, 89, 23],
[3, 6, 77, 89, 23],
[2, 7, 9]
],
"suitableStaff": [
[22, 44],
[22, 44],
[4]
],
"suitableRooms": [
[89, 23],
[22, 44],
[2]
],
"ValidStartDate": true
}, {
"availableStaff": [
[0, 1, 4, 3, 5, 9, 22, 44],
[0, 1, 4, 3, 5, 9, 22, 44],
[4, 8, 7]
],
"availableRooms": [
[3, 6, 77, 89, 23],
[3, 6, 77, 89, 23],
[2, 7, 9]
],
"suitableStaff": [
[22, 44],
[22, 44],
[4]
],
"suitableRooms": [
[89, 23],
[22, 44],
[2]
],
"ValidStartDate": true
}, {
"availableStaff": [
[0, 1, 4, 3, 5, 9, 22, 44],
[0, 1, 4, 3, 5, 9, 22, 44],
[4, 8, 7]
],
"availableRooms": [
[3, 6, 77, 89, 23],
[3, 6, 77, 89, 23],
[2, 7, 9]
],
"suitableStaff": [
[22, 44],
[22, 44],
[4]
],
"suitableRooms": [
[89, 23],
[22, 44],
[2]
],
"ValidStartDate": false
}, {
"availableStaff": [
[0, 1, 4, 3, 5, 9, 22, 44],
[0, 1, 4, 3, 5, 9, 22, 44],
[4, 8, 7]
],
"availableRooms": [
[3, 6, 77, 89, 23],
[3, 6, 77, 89, 23],
[2, 7, 9]
],
"suitableStaff": [
[22, 44],
[22, 44],
[4]
],
"suitableRooms": [
[89, 23],
[22, 44],
[2]
],
"ValidStartDate": true
}]
}


On my site I have a custom made calendar view of a week, which is created from a template, starting from Monday to Friday. By default, there are 2 weeks but a user can change how many weeks they want to see. Each week is split up by a div with a unique id
"solCol0"
,
"solCol1"
etc....

enter image description here

I then loop through the number of weeks selected, in this case we have
2
.
And then loop through the length of
solution
which is
10
in this case.
I only want to loop 5 times (for each day of week) and then after 5 loops, increment the columdId by 1 to append the details onto the next week, for example the first 5 loops are appended to
"solCol0"
and then the next 5 are appended to "
solCol1
" and if the user has selected more than 2 weeks, to 3,
solution
length would increase to 15 so the next 5 loops would append to "'solCol2"` etc...

Sorry if this isn't very clear, in general I just have to increment a value after every 5 loops. Any help would be greatly appreciated.

loadSolutionStartRows: function(dates, solution) {
var self = this;
for (var i = 0; i < dates.length; i++) {
var columnId = "#solCol" + i;
var startDate = moment(dates[i], 'Do MMM');
var rowDates = [];
var iterate = 5;
for (var d = 0; d < solution.days.length; d++) {
//Every 5 loops - columnId = "#solCol" + i + 1;
rowDates.push(moment(startDate).format('ddd (Do MMM)'));
startDate.add(1, 'days');
var selectedDate = rowDates[d];

var statusClass;
var statusIconClass;
if (solution.days[d].ValidStartDate === true) {
statusClass = "sxpTableHeaderIconGreenStatus";
statusIconClass = "octicon " + "octicon-check";
}
if (solution.days[d].ValidStartDate === false) {
statusClass = "sxpTableHeaderIconRedStatus";
statusIconClass = "octicon " + "octicon-x";
}


$(columnId).append(self.solutionTableRow({
rowId: i + 1,
date: selectedDate,
statusClass: statusClass,
statusIconClass: statusIconClass,
trainerCountEarly: 1,
trainerListEarly: 1,
roomCountEarly: 1,
roomListEarly: 1,
trainerCountLate: 1,
trainerListLate: 1,
roomCountLate: 1,
roomListLate: 1
}));
}
}
}

},


With the code above I achieve the image below, it adds 10 days instead of the 5 as I want.

enter image description here

Answer

You will want to have a variable that gets incremented every 5th iteration, so you need to check if the index is a multiple of 5:

var foo = 0;

for (var i; i < someLength; ++i) {
  if (i % 5 === 0) {
    foo++;
  }
}

This uses the modulus operator to get the remainder of i / 5. If its 0, then we know the index is a multiple of 5.

Note that this is strictly unnecessary, one can alternatively just do

Math.floor(someLength / 5);

Or more succinctly:

someLength / 5 | 0;

Both will truncate the result of the division to an integer, and you will know how many 5's someLength contains.

Comments