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

177
Visualizações
A question about "getTimeDay" in functions -comparisons-conditions

sorry to ask the simple question in this advanced platform full of great coders, please point out what i have to correct my code.Thank you so much!

1.here is the code requirements:

Declare a function getTimeOfDay. For reference:

4:00 AM (inclusive) - 12:00 PM (exclusive): morning

12:00 PM (inclusive) - 5:00 PM (exclusive): afternoon

5:00 PM (inclusive) - 8:30 PM (exclusive): evening

8:30 PM (inclusive) - 4:00 AM (exclusive): night

/**

  • @param {1|2|3|4|5|6|7|8|9|10|11|12} ??? - the hour (12-hour style)
  • @param {number} ??? - the number of minutes past the hour
  • @param {'AM'|'PM'} ??? - "AM" or "PM"
  • @returns {'morning'|'afternoon'|'evening'|'night'} the rough "time of day"

*/

2.here is the TEST that i am supposed to pass:

actual = getTimeOfDay(4, 0, "AM");

expected = "morning";

if (actual === expected) {

console.log("Yay! Test PASSED.");

} else {

console.error("Test FAILED. Keep trying!");

console.log(" actual: ", actual);

console.log(" expected: ", expected); }

actual = getTimeOfDay(3, 59, "AM");

expected = "night";

if (actual === expected) {

console.log("Yay! Test PASSED.");

} else {

console.error("Test FAILED. Keep trying!");

console.log(" actual: ", actual);

console.log(" expected: ", expected);

}

3.here is my code:

    function getTimeOfDay(hr,num,AM){

      if(hr === AM){

        if(hr >= 4 && hr < 12){

         if(num >=0 && num <= 59){

           return "morning";
       }
       }
     }

     else if(hr >= 12 && hr < 5)

          return "afternoon";

     else if(hr >=5 && hr < 8){

     if(num >=0 && num <= 30)

        return "evening";

    else

        return "night";

   }
 }

4.here is the error message from my code:

enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The main cause of the error is that condition if(hr === AM), I have modified the code a little bit.

function getTimeOfDay(hr,num,period){

      if(period == "AM" ){

        if(hr >= 4 && hr < 12){

         if(num >=0 && num <= 59){

           return "morning";
         }
       }
       else{
        return "night";
       }
     }

     else{

     if(hr >= 12 && hr < 5)

          return "afternoon";

     else if(hr >=5 && hr < 8){

     if(num >=0 && num <= 30)

        return "evening";

    else

        return "night";

   }
}
 }

Note: conditions maybe could be written in a better way, I didn't check their correctness but the provided test cases are passed, I just wanted to highlight the cause of error for you.

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