+ 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); }
13 Respostas
+ 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.
+ 2
If (hour >= 0 && hour <= 12) {
console.log('am');
} else {
console.log('pm');
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
0
Simisola Osinowo maybe 12 denoted pm?
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
0
Kingsley Clement Kefas
It still did not work
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()
0
Kingsley Clement Kefas it still did not work
0
You can just go directly with
function main() {
var hour = parseInt(readLine(), 10);
if (hour<=12) {
console.log("am");
} else
console.log("pm");
}
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);
}
0
function main() {
var hour = parseInt(readLine(), 10);
// Your code goes here
noonormidnight = hour >= 0 && hour <= 12?"am":"pm";
console.log(time)
}
0
function main() {
var hour = parseInt(readLine(), 10);
// my try
var clock = (hour>=0 && hour <= 12) ? 'am':'pm';
console.log(clock)
}
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);