Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

235
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda