我的:
// Write - Your - Code
const hours=Number(prompt("Enter the hours:"));
const minutes=Number(prompt("Enter the minutes:"));
const seconds=Number(prompt("Enter the seconds:"));
if(seconds <60 && seconds >=0 && minutes <60 && minutes>=0 && hours >=0 && hours <24)
{
if(seconds <59 )
{
console.log(`In a second,time will be ${hours} hours,${minutes} minutes,and ${seconds+1} seconds`);
}
else if(seconds === 59 && minutes !==59)
{
console.log(`In a second,time will be ${hours} hours,${minutes+1} minutes,and 0 seconds`);
}
else if(seconds === 59 && minutes ===59 && hours !=23)
{
console.log(`In a second,time will be ${hours+1} hours,0 minutes,and 0 seconds`);
}
else if(seconds === 59 && minutes ===59 && hours ===23)
{
console.log(`In a second,time will be 0 hours,0 minutes,and 0 seconds`);
}
}
else
{
console.log("the entered value is invalid");
}
官方的:
let hours = Number(prompt("Enter the hours:"));
let minutes = Number(prompt("Enter the minutes:"));
let seconds = Number(prompt("Enter the seconds:"));
// We start by testing errors cases
if (
hours >= 0 &&
hours <= 23 &&
minutes >= 0 &&
seconds >= 0 &&
seconds <= 59
) {
seconds++; // Incrementing the seconds
if (seconds === 60) {
// Next minute: seconds are reset to 0
seconds = 0;
minutes <= 59 &&
minutes++;
if (minutes === 60) {
// Next hour: minutes are reset to 0
minutes = 0;
hours++;
if (hours === 24) {
// Next second is midnight
hours = 0;
}
}
}
console.log(
`In a second, time will be ${hours} hours, ${minutes} minutes and ${seconds} seconds.`
);
} else {
console.log("Incorrect input!");
}