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) }

15th Nov 2020, 3:06 PM
Chevy Jackson
Chevy Jackson - avatar
10 Respostas
+ 6
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
15th Nov 2020, 3:22 PM
Davide
Davide - avatar
+ 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.
15th Nov 2020, 3:24 PM
ChaoticDawg
ChaoticDawg - avatar
+ 4
You are welcome đŸ€—
15th Nov 2020, 3:27 PM
Davide
Davide - avatar
+ 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?
17th Feb 2021, 11:12 PM
Sergii Poberezhniuk
Sergii Poberezhniuk - avatar
+ 2
Got it. Thanks guys! đŸ‘đŸŸ
15th Nov 2020, 3:25 PM
Chevy Jackson
Chevy Jackson - avatar
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) } }
29th May 2021, 12:48 AM
John Blanton
John Blanton - avatar
0
var meridiem = (hour <= 12 && hour <= 24) ? 'am':'pm'; console.log(meridiem); }
29th Aug 2021, 2:24 AM
Jatinder Singh Jassar
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'); } }
15th Nov 2021, 9:48 AM
MD RAKIBUL HASSAN NAYON
MD RAKIBUL HASSAN NAYON - avatar
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'); } }
13th May 2022, 6:39 PM
Oxunjon Pirov
Oxunjon Pirov - avatar