I am writing a function to 'calculate' the dates of a working week based on the current date. Console.log of the array's item is correct in the cycle, but when I print the content of the array at the end of the cycle, all the items have the same value. I can't figure out what's wrong in my logic.
Any hing is much appreciated.
function calculateWorkingDays(){
var weekDates = ["0","1","2","3","4","5","6"];
var currentDate = new Date();
var weekDay = currentDate.getDay();
console.log("Initial weekDay: " + weekDay);
for (var i=0; i<7; i++){
console.log(i);
//check for Sunday (0)
if (weekDay==0){
weekDates[currentDate.getDay()] = currentDate;
//console.log("if i=0: day" + currentDate.getDay());
console.log("date: " + currentDate);
console.log("day: " + currentDate.getDay());
console.log("weekDates" + currentDate.getDay() + " " + weekDates[currentDate.getDay()]);
//set to Monday (1)
weekDay = 1;
currentDate.setDate(currentDate.getDate()-6);
} else {
if (weekDay<6) {
weekDates[currentDate.getDay()] = currentDate;
console.log("date: " + currentDate);
console.log("day: " + currentDate.getDay());
console.log("weekDates" + currentDate.getDay() + " " + weekDates[currentDate.getDay()]);
weekDay = weekDay + 1;
} else {
weekDates[currentDate.getDay()] = currentDate;
console.log("date: " + currentDate);
console.log("day: " + currentDate.getDay());
console.log("weekDates" + currentDate.getDay() + " " + weekDates[currentDate.getDay()]);
// set to Sunday (0)
weekDay = 0 ;
}
currentDate.setDate(currentDate.getDate()+1);
}
}
console.log(weekDates.toString());
}
weekDay) byiotherwise the code isnt changing on each run though?