Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
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)))

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!