+ 3

Please explain this code, this is not working, I don't know why?

let hour = 14 switch (hour) { case hour >= 6 && hour < 12: console.log('Good Morning'); break; //if hour is between 6am to 12pm then print 'Good Morning' case hour >= 12 && hour <= 18: console.log('Good Afternoon'); break; //if hour is between 12pm to 6pm then print 'Good Morning' default: console.log ('Good Evening'); break; //otherwise just print 'Good Evening' } Please see this code tell me if there are any syntax errors or some other errors because I'm always getting 'Good Evening', Help me.

13th Nov 2020, 8:08 AM
R. Krishna
1 Antwort
+ 2
Use of relational operators in case statement is not allowed. Relational operators: >, <, >=, etc. //Your code be like let hour = 14 switch (hour) { case 6: case 7: case 8: case 9: case 10: case 11: console.log('Good Morning'); break; //if hour is between 6am to 12pm then print 'Good Morning' case 12: case 13: case 14: case 15: case 16: case 17: case 18: console.log('Good Afternoon'); break; //if hour is between 12pm to 6pm then print 'Good Morning' default: console.log ('Good Evening'); break; //otherwise just print 'Good Evening' }
13th Nov 2020, 8:15 AM
ツSampriya😘ツ
ツSampriya😘ツ - avatar