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

236
Vistas
JavaScript Array interview question; couldn't find the bug

A friend of mine was asked the question below during a live interview and was given 4 minutes to answer it.

QUESTION: Without opening any other browser or using code editor, analyze the code below and give your verdict if it is correct and good to go, if not, debug the code and explain your answer.

const weekDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];

for (let i = 0; i < weekDays.length; i++){
    if (i === 'Wednesday'){
        console.log('Today is Wednesday, join board meeting by 10:00am');
        continue;
    }
    if (i === 'Saturday'){
        console.log("Today is Saturday, weekend is great!");
        break;
    }
    console.log(weekDays[i]);
}
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Just take a for ... of statement, because the array could have days in different order or start.

And for really good code, take a semantic name day instead of i.

const weekDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];

for (const i of weekDays) {
    if (i === 'Wednesday') {
        console.log('Today is Wednesday, join board meeting by 10:00am');
        continue;
    }
    if (i === 'Saturday') {
        console.log("Today is Saturday, weekend is great!");
        break;
    }
    console.log(i);
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

I know you provided your own answer, but this code could easily be cleaned up further to reduce bugs:

for (let i = 0; i < weekDays.length; i++) {
    if (i === 3) {
        console.log('Today is Wednesday, join board meeting by 10:00am');
    } else if (i === 6) {
        console.log("Today is Saturday, weekend is great!");
    } else {
        console.log(weekDays[i]);
    }
}

Still not a fan of the magic numbers. Perhaps i === Day.Wednesday or weekDays[i] === Day.Wednesday depending on what's expected to change.

about 4 years ago · Juan Pablo Isaza Denunciar

0

for (let i = 0; i < weekDays.length; i++){
    if (i === 3){
        console.log('Today is Wednesday, join board meeting by 10:00am');
        continue;
    }
    if (i === 6){
        console.log("Today is Saturday, weekend is great!");
        break;
    }
    console.log(weekDays[i]);
}

EXPLANATION: There are bugs in line 2 => (i === 'Wednesday') and line 6 => (i === 'Saturday'). The 'Wednesday' in line 2 should be changed to 3 while the 'Saturday in line' 6 should be changed to 6.

Reason: An array loops through its values by indexing, e.g 1, 2, 3,... and not by the name of the values. So the array, by looping through doesn't understand 'Wednesday' and 'Saturday' but its index which are 3 and 6 respectively.

about 4 years ago · Juan Pablo Isaza Denunciar
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