0

Logical or Boolean Operators

Stuck on the question below. Learning through JavaScript. I’m aware you can use the if else statements but I’m not the far into the lesson. I’m sure I’m doing something wrong. I’m new to this so take it easy on me lol Given a clock that measures 24 hours in a day. Write a program, that takes the hour as input. If the hour is in the range of 0 to 12, output am to the console, and output pm if it's not. function main() { var hour = parseInt(readLine(), 10) console.log(hour && 12) }

11/15/2020 3:06:23 PM

Chevy Jackson

+5

A comparison of two values with && returns 1 if both of the values are non-zero, otherwise returns 0 So printing the comparison can result only in printing either 0 or 1. In particular you will print 0 if hour == 0 and print 1 in any other case. You need to use an if() statement to solve the problem

+4

You just need to use an if-else statement. Check if the value of hour is greater than 12. If so then use console.log() to output "pm", otherwise output "am". If you haven't done enough in your lessons to understand, then I would suggest you do them first.

+4

You are welcome 🤗

+4

if the comments are true it means that the task was given prior to the "if/else" lesson... why giving the learners this pointless & endless loop then? Else, if it was not done on purpose, then it's a serious and misleading disorder in JS course... Right?

+2

Got it. Thanks guys! 👍🏾

+1

I"m not sure why my code doesn't work, can you guys help? function main() { var hour = parseInt(readLine(), 10); // Your code goes here if hour =12>(console.log(pm)) else (console.log(am)) } Thanks

0

No need for if-else yet. It hasn't been taught but it will work. function main() { var hour = parseInt(readLine(), 10) var ampm = (hour <= 12) ? "am": "pm"; console.log(ampm) } }

0

var meridiem = (hour <= 12 && hour <= 24) ? 'am':'pm'; console.log(meridiem); }

0

//Simply Use this code easy to understand for all CHILL function main() { var hour = parseInt(readLine(), 10); // Your code goes here if (hour <= 12){ console.log('am'); } else { console.log ('pm'); } }

0

//Simply Use this code easy to understand for all CHILL function main() { var hour = parseInt(readLine(), 10); // Your code goes here if(hour>=0 && hour<=12) { console.log('am'); } else { console.log('pm'); } }