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) }
9 Answers
+ 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
+ 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! šš¾
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');
}
}