javascript:输出下一秒的时间

我的:

// 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!");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值