Home > OS >  Wrong elements added to object in for loop
Wrong elements added to object in for loop

Time:12-20

I’m trying to make a work schedule generator in Node. In this component, I’m trying to assemble a random schedule for a specific month, using a template and a random number generator. I’m also adding a date to the final objects.

Minimal reproducible example:

var templates = [
  {
    "name": "test",
    "values": [ "val1", "val2", "val3" ],
    "date": ""
  },
  {
    "name": "test2",
    "values": [ "val4", "val5", "val6" ],
    "date": ""
  },
  {
    "name": "test3",
    "values": [ "val7", "val8", "val9" ],
    "date": ""
  }
];

function getRandomInt(max) {
  return Math.floor(Math.random() * max);
}

function getDaysInMonth(month, year) {
  var date = new Date(year, month, 1);
  var days = [];
  
  while (date.getMonth() === month) {
    var day = new Date(date).toLocaleDateString();
    
    days.push(day);
    date.setDate(date.getDate()   1);
  }
  
  return days;
}

const generateWorkSchedule = (month, year) => {
  var days = getDaysInMonth((month - 1), year);
  var schedule = []

  console.log("days", days);

  for (var i = 0; i < days.length; i  ) {
    var daySchedule = {};
    
    daySchedule = templates[getRandomInt(3)]
    daySchedule.date = days[i]
    console.log(`days[${i}]`, days[i]);
    schedule.push(daySchedule)
  }

  return schedule
}

console.log(generateWorkSchedule(12, 2021));

Output (dates change every run):

[
  {
    name: 'test3',
    values: [ 'val7', 'val8', 'val9' ],
    date: '2021. 11. 30.'
  },
  {
    name: 'test',
    values: [ 'val1', 'val2', 'val3' ],
    date: '2021. 11. 29.'
  },
  {
    name: 'test3',
    values: [ 'val7', 'val8', 'val9' ],
    date: '2021. 11. 30.'
  },
  {
    name: 'test',
    values: [ 'val1', 'val2', 'val3' ],
    date: '2021. 11. 29.'
  },
  {
    name: 'test2',
    values: [ 'val4', 'val5', 'val6' ],
    date: '2021. 11. 28.'
  },
  {
    name: 'test3',
    values: [ 'val7', 'val8', 'val9' ],
    date: '2021. 11. 30.'
  },
  {
    name: 'test',
    values: [ 'val1', 'val2', 'val3' ],
    date: '2021. 11. 29.'
  },
  {
    name: 'test3',
    values: [ 'val7', 'val8', 'val9' ],
    date: '2021. 11. 30.'
  },
  {
    name: 'test3',
    values: [ 'val7', 'val8', 'val9' ],
    date: '2021. 11. 30.'
  },
  {
    name: 'test2',
    values: [ 'val4', 'val5', 'val6' ],
    date: '2021. 11. 28.'
  },
  {
    name: 'test3',
    values: [ 'val7', 'val8', 'val9' ],
    date: '2021. 11. 30.'
  },
  {
    name: 'test3',
    values: [ 'val7', 'val8', 'val9' ],
    date: '2021. 11. 30.'
  },
  {
    name: 'test2',
    values: [ 'val4', 'val5', 'val6' ],
    date: '2021. 11. 28.'
  },
  {
    name: 'test3',
    values: [ 'val7', 'val8', 'val9' ],
    date: '2021. 11. 30.'
  },
  {
    name: 'test',
    values: [ 'val1', 'val2', 'val3' ],
    date: '2021. 11. 29.'
  },
  {
    name: 'test3',
    values: [ 'val7', 'val8', 'val9' ],
    date: '2021. 11. 30.'
  },
  {
    name: 'test3',
    values: [ 'val7', 'val8', 'val9' ],
    date: '2021. 11. 30.'
  },
  {
    name: 'test2',
    values: [ 'val4', 'val5', 'val6' ],
    date: '2021. 11. 28.'
  },
  {
    name: 'test',
    values: [ 'val1', 'val2', 'val3' ],
    date: '2021. 11. 29.'
  },
  {
    name: 'test3',
    values: [ 'val7', 'val8', 'val9' ],
    date: '2021. 11. 30.'
  },
  {
    name: 'test',
    values: [ 'val1', 'val2', 'val3' ],
    date: '2021. 11. 29.'
  },
  {
    name: 'test2',
    values: [ 'val4', 'val5', 'val6' ],
    date: '2021. 11. 28.'
  },
  {
    name: 'test3',
    values: [ 'val7', 'val8', 'val9' ],
    date: '2021. 11. 30.'
  },
  {
    name: 'test',
    values: [ 'val1', 'val2', 'val3' ],
    date: '2021. 11. 29.'
  },
  {
    name: 'test',
    values: [ 'val1', 'val2', 'val3' ],
    date: '2021. 11. 29.'
  },
  {
    name: 'test2',
    values: [ 'val4', 'val5', 'val6' ],
    date: '2021. 11. 28.'
  },
  {
    name: 'test2',
    values: [ 'val4', 'val5', 'val6' ],
    date: '2021. 11. 28.'
  },
  {
    name: 'test2',
    values: [ 'val4', 'val5', 'val6' ],
    date: '2021. 11. 28.'
  },
  {
    name: 'test',
    values: [ 'val1', 'val2', 'val3' ],
    date: '2021. 11. 29.'
  },
  {
    name: 'test3',
    values: [ 'val7', 'val8', 'val9' ],
    date: '2021. 11. 30.'
  }
]

The problem is, the final objects have random dates instead of dates in order. I tried it without the random number generator, the problem still stands.

The console.log outputs are looking good.

So it seems to be good, but the final results have random dates. I’m ether doing something really wrong, or node or JS have some weird low level stuff I dont know about.

CodePudding user response:

This was a tricky one, for sure! I ran the code several times and I think I see what the problem is. In your generateWorkSchedule function, you are retrieving the template from the array and setting it to a variable. Then, your function updates that object (updates the actual template with daySchedule.date = days[i]). Each iteration through the loop in generateWorkSchedule resets the template's date to be the last day. For objects, JavaScript uses "pass by reference" and you are not making a copy of your work schedule template. Try this function (though, I'm sure there are other ways of solving this problem, this was one way that worked for me):

const generateWorkSchedule = (month, year) => {
  var days = getDaysInMonth((month - 1), year);
  var schedule = []
  for (var i = 0; i < days.length; i  ) {
    var template = templates[getRandomInt(3)];
    var daySchedule = { 
      name: template.name,
      values: template.values,
      date: days[i],
    }
    schedule.push(daySchedule)
  }

  return schedule
}
  • Related