Noon or Midnight | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Noon or Midnight

I’m lost once again, below is the question and the given code to start with. Any help? I tried to make Variables for Am and Pm separately but it did not work. Var Am <=11 Var Pm >= 12 But I don’t think I’m on the write path. 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) // Your code here }

22nd Nov 2020, 6:35 PM
Andrew Medrano
Andrew Medrano - avatar
10 Answers
+ 4
//not sure but this maybe work,... Based on your code function main(){ var hour = parseInt(readLine(), 10); // Your code here if(hour <= 12){ console.log("am"); } else{ console.log("pm"); } }
22nd Nov 2020, 7:01 PM
Sousou
Sousou - avatar
+ 3
Hi! I hope I'm not late at all, but I think you are talking about the practice of Lesson 10.1 Part 1, I had trouble solving it, but after looking further on the Lesson I have figured out it is possible to do with "Conditional (Ternary) Operator" This is what the syntax would look like: variable = (condition) ? value1: value2 And applied on the problem, it would look like this: var NoonOrMidnight = (hour <= 12) ? "am" : "pm"; console.log(NoonOrMidgnight) With the full code provided by the site: function main() { var hour = parseInt(readLine(), 10); // Your code goes here var NoonOrMidgnight = (hour <= 12) ? "am" : "pm"; console.log(NoonOrMidnight) } Hope this helps someone!
10th Jul 2021, 12:22 AM
José Luis
José Luis - avatar
+ 2
var hours = prompt("Please add the time"); // This is if you use Logical Operators (hours <= 12 ) && console.log("AM"); (hours > 12 ) && console.log("PM"); (hours <= 12) ? console.log("AM") : console.log("PM"); // This is if you use conditional if (hours <= 12 ){ console.log("AM") } else { console.log("PM") }
23rd Mar 2021, 11:38 PM
Toka ElTorkey
+ 1
Andrew Medrano Your variable declaration is wrong there.. In shloud be var am=11, pm=12; (example) And missed your message that am not activated my account so I can't reply via msg.. Not seen also.. Is it about this question..? After reading this, I understood that this question asked before if-else lesson.. But there they will specify what concept question about.. But if your sure about this is not in a proper structure, you can mailto : info@sololearn.com saying it as a suggestion or complaint.. That helps others..
22nd Nov 2020, 7:44 PM
Jayakrishna 🇮🇳
+ 1
You're welcome... My DM don't works now.. If I see it q/a, I try to give you answer if I know it... With the community, for a proper question mostly 90+% gets a quick answers.. Happy learning..
22nd Nov 2020, 7:53 PM
Jayakrishna 🇮🇳
+ 1
var hour = prompt("Hour: "); if (hour<=12) { console.log("Its AM"); } else if (hour<=24) { console.log("Its PM"); } else { console.log("Error!"); } hope it helps ! xd
25th Nov 2020, 1:40 AM
Yun Jo Subba
Yun Jo Subba - avatar
+ 1
// Your code here if(hour < 12){ console.log("am"); } else if(hour>=12) { console.log("pm"); } These is good answer like for me guys :)
1st Sep 2023, 11:18 AM
yassine lafraouzi
yassine lafraouzi - avatar
0
Sousou it solved it but im not sure thats how they intended me to do so because i haven’t learned “if” but regardless thank you.
22nd Nov 2020, 7:04 PM
Andrew Medrano
Andrew Medrano - avatar
0
Jayakrishna🇮🇳 thank you, yes the message was about this problem, i found yoj to be the most helpful on my last issue so i thought i’d ask you. i appreciate it very much.
22nd Nov 2020, 7:46 PM
Andrew Medrano
Andrew Medrano - avatar
0
(hour <= 12) && console.log("am"); (hour > 12) && console.log("pm"); If you use the && operator all operands have to be true. If (hour <= 12) is true then console.log("am"); will run, If (hour <= 12) is false then console.log("am"); wont run.
22nd Feb 2021, 1:30 AM
Moithuti