这看起来正确吗?
所以我不久前做了这个(我相信是在这里的帮助下,但我不记得了——不相关)来上课。它应该根据一天中的时间返回一条消息,然后返回一条随机消息。随机消息部分工作正常。这是我感到困惑的一天中的时间消息。告诉我你的想法...看起来合适吗?
var hr = new Date();
if (hr < 12) {
alert("Good Morning");
}
if ((hr - 12) < 6) {
alert("Good Afternoon");
}
if (hr >= 18) {
alert("Good Evening");
}
msg = new Array
msg[1]="how are you"
msg[2]="whazzup"
msg[3]="how have you been"
msg[4]="how has your day been going"
msg[5]="hey y'all"
random_num = (Math.round((Math.random()*4)+1))
alert(msg[random_num]);
非常感谢所有帮助和建议。谢谢。
So I made this a while ago (I believe with help from here but I can't remember -irrelevant) for class. Its supposed to return a message based on the time of day and then a random message. The random message part works fine. Its the Time of Day message I'm confused about. Tell me what you think... does it look right?
var hr = new Date();
if (hr < 12) {
alert("Good Morning");
}
if ((hr - 12) < 6) {
alert("Good Afternoon");
}
if (hr >= 18) {
alert("Good Evening");
}
msg = new Array
msg[1]="how are you"
msg[2]="whazzup"
msg[3]="how have you been"
msg[4]="how has your day been going"
msg[5]="hey y'all"
random_num = (Math.round((Math.random()*4)+1))
alert(msg[random_num]);
All help and suggestions are greatly appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该调用
getHours()
来返回小时。您的hr
实际上是一个Date
对象:You should be calling
getHours()
to return the hour. Yourhr
is actually aDate
object:对于数组:
For the array:
因为您将相同的表达式与多个结果进行比较,所以我建议使用 switch 语句,因为它更干净,并且提供了更大的灵活性,而不是一堆带有 else if 之类的意大利面条代码。
Because your comparing the same expression to multiple outcomes I would recommend a switch statement because it is much cleaner, and provides greater flexibility instead of a bunch of spaghetti code, with else if's and stuff.