'Javascript While Loop exits before finishing execution
I am trying to create a simple program:
Input: a date1
Output: a date2 that is 4 weekdays from date1 (going forward in time)
I execute the code and only receive a date2 that is 1 day away.
After some investigation, I noticed that my while loop is only executing once, not 4 times like I specified in let freeDays = 4;.
Could someone explain what I am doing wrong? Why is the while loop not going through 4 times?
Date.prototype.addDays = function(days) {
this.setDate( this.getDate() + days);
return this;
Date.prototype.isWeekday = function(){
return !((this.getDay() == 0)||(this.getDay() == 6));
}
function getLastFreeDay(date1){
let freeDays = 4;
while (freeDays>0){
if(date1.isWeekday()){
freeDays--;
date1.addDays(1);
}
else{
date1.addDays(1);
}
return date1;
}
}
console.log(getLastFreeDay(new Date(2022, 1, 22)))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
