I'm beginning javascript and i do some exercise to train my self, but i've a
problem.
My code had to ask the time to the user and give the time one seconde after
the time entered.
And my code shows my always "temps erroné" (that mean ~wrong time).
Here's my code :
向用户询问时间
var hour = Number(prompt("heure actuelle?"));
var minute = Number(prompt("minute actuelle?"));
var seconde = Number(prompt("seconde actuelle?"));
//这个函数就像一个控件,如果输入的时间是错误的函数 是假的
function isTimeValid(hour, minute, seconde){
if((hour >= 24) || (hour < 0)) {
return false
}
if((minute < 0) || (minute >= 60)) {
return false
}
if((seconde >= 60) || (seconde < 0)) {
return false
}
else {
return true
}
}
此功能修改时间
function displayNextHour(hour, minute, seconde){
if((seconde >= 0) && (seconde < 59)) {
seconde++;
} else if(seconde === 59) {
seconde = 0
minute++
}
if(minute === 60) {
minute = 0
hour++
}
if(hour === 24) {
hour = 0
}
var time = "Il est " + hour + "h " + minute + "m " + seconde + "s";
return time
}
最后一部分午餐功能
if(isTimeValid()){
console.log(displayNextHour(hour, minute, seconde))
}else{
console.log("temps erroné")
}
所以,如果你能帮助我,那对我的javascript学习非常有帮助:D
答案 0 :(得分:0)
您没有将参数传递给函数
替换
if(isTimeValid())
与
if(isTimeValid(hour, minute, seconde))
答案 1 :(得分:0)
你没有传递像迈克所说的论点,你的代码中还有另一个错误。 您可以控制“isTimeValid”中的小时数(= 24)和分钟数(= 60),但下一个小时数 函数控制此参数时出错。下面的代码修复了它。
function displayNextHour(hour, minute, seconde){
if((seconde >= 0) && (seconde < 59)) {
seconde++;
} else if(seconde === 59) {
seconde = 0
minute++
}
if(minute === 59) {
minute = 0
hour++
}
if(hour === 23) {
hour = 0
}
var time = "Il est " + hour + "h " + minute + "m " + seconde + "s";
return time
}