Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

118
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda