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

172
Views
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 answers
Answer question

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 Report
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!