Trying to solve noon or midnight in JavaScript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Trying to solve noon or midnight in JavaScript

how do i make my code output am and pm Time flies when you’re having fun. 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. Sample Input 13 Sample Output pm my attempt function main() { var hour = parseInt(readLine(), 10); // Your code goes here console.log(hour<=24); }

29th Apr 2021, 10:45 AM
Simisola Osinowo
Simisola Osinowo - avatar
13 Answers
+ 5
Can someone explain to me why we get these practices with if, else statements when we haven't even learned these concepts yet? I'm happy to practice the things I've learned but how are we supposed to practice concepts that haven't been taught to us yet? Is the idea that we research this online and formulate an answer or.... I don't know. I'm new to Sololearn so maybe I'm missing something.
19th Jul 2021, 3:36 PM
Bob van Eck
Bob van Eck - avatar
+ 2
If (hour >= 0 && hour <= 12) { console.log('am'); } else { console.log('pm');
29th Apr 2021, 10:55 AM
Makar Mikhalchenko
Makar Mikhalchenko - avatar
0
Like this Makar Mikhalchenko function main() { var hour = parseInt(readLine(), 10); // Your code goes here If (hour >= 0 && hour <= 12) { console.log('am'); } else { console.log('pm'); } this did not work
29th Apr 2021, 11:08 AM
Simisola Osinowo
Simisola Osinowo - avatar
0
Simisola Osinowo maybe 12 denoted pm?
29th Apr 2021, 11:13 AM
你知道規則,我也是
你知道規則,我也是 - avatar
0
Simisola Osinowo Do this function main() { var hour = parseInt(readLine(), 10); if (hour >= 0 && hour < 12) { console.log('am'); } else { console.log('pm'); } } main() NOTE: PM starts by exactly 12, so the last hour of AM is 11
29th Apr 2021, 10:19 PM
Kingsley Clement Kefas
Kingsley Clement Kefas - avatar
0
Kingsley Clement Kefas It still did not work
1st May 2021, 12:45 PM
Simisola Osinowo
Simisola Osinowo - avatar
0
Simisola Osinowo You should replace "readLine()" on line 2 with "prompt()". Your code will look like this: function main() { var hour = parseInt(prompt(), 10); if (hour >= 0 && hour < 12) { console.log('am'); } else { console.log('pm'); } } main()
1st May 2021, 2:52 PM
Kingsley Clement Kefas
Kingsley Clement Kefas - avatar
0
Kingsley Clement Kefas it still did not work
9th May 2021, 12:03 PM
Simisola Osinowo
Simisola Osinowo - avatar
0
You can just go directly with function main() { var hour = parseInt(readLine(), 10); if (hour<=12) { console.log("am"); } else console.log("pm"); }
18th Jul 2021, 3:57 AM
Era Marie Tolero
Era Marie Tolero - avatar
0
HI this is working: function main() { var hour = parseInt(readLine(), 10); // Your code goes here var f = (hour<=12) ? "am" : "pm"; console.log(f); }
21st Jul 2021, 9:16 PM
Dániel Varjaskéri
0
function main() { var hour = parseInt(readLine(), 10); // Your code goes here noonormidnight = hour >= 0 && hour <= 12?"am":"pm"; console.log(time) }
21st Aug 2022, 11:21 AM
abd R
abd R - avatar
0
function main() { var hour = parseInt(readLine(), 10); // my try var clock = (hour>=0 && hour <= 12) ? 'am':'pm'; console.log(clock) }
1st May 2023, 5:38 PM
Kenshinn Ackerman
Kenshinn Ackerman - avatar
0
this work perfectly let hour = parseInt(readLine(), 10); //your code goes here let time = 24; time = (hour >=0 && hour <= 11 ) ? "am" : "pm"; console.log(time);
6th Jan 2024, 11:38 AM
Sarkodie Andrews
Sarkodie Andrews - avatar