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
/**
*/
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:
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.