这看起来正确吗?

发布于 2024-12-13 07:24:35 字数 554 浏览 2 评论 0原文

所以我不久前做了这个(我相信是在这里的帮助下,但我不记得了——不相关)来上课。它应该根据一天中的时间返回一条消息,然后返回一条随机消息。随机消息部分工作正常。这是我感到困惑的一天中的时间消息。告诉我你的想法...看起来合适吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

够钟 2024-12-20 07:24:35

您应该调用 getHours() 来返回小时。您的 hr 实际上是一个 Date 对象:

var d = new Date();
var hr = d.getHours();

// Etc...
if (hr < 12) {
    alert("Good Morning");
}
if ((hr - 12) < 6) {
    alert("Good Afternoon");
}
if (hr >= 18) {
    alert("Good Evening");
}

You should be calling getHours() to return the hour. Your hr is actually a Date object:

var d = new Date();
var hr = d.getHours();

// Etc...
if (hr < 12) {
    alert("Good Morning");
}
if ((hr - 12) < 6) {
    alert("Good Afternoon");
}
if (hr >= 18) {
    alert("Good Evening");
}
南风几经秋 2024-12-20 07:24:35
var hr = (new Date()).getHours();
if (hr < 12)
{
   alert("Good Morning");
}
else if (hr < 18)
{
   alert("Good Afternoon");
}
else if (hr >= 18)
{
   alert("Good Evening");
}

对于数组:

msg = [null]; //start off with index 0 as null
msg.push("how are you");
msg.push("whazzup");
msg.push("how have you been");
msg.push("how has your day been going");
msg.push("hey y'all");
var hr = (new Date()).getHours();
if (hr < 12)
{
   alert("Good Morning");
}
else if (hr < 18)
{
   alert("Good Afternoon");
}
else if (hr >= 18)
{
   alert("Good Evening");
}

For the array:

msg = [null]; //start off with index 0 as null
msg.push("how are you");
msg.push("whazzup");
msg.push("how have you been");
msg.push("how has your day been going");
msg.push("hey y'all");
迷爱 2024-12-20 07:24:35

因为您将相同的表达式与多个结果进行比较,所以我建议使用 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.

飞烟轻若梦 2024-12-20 07:24:35
// proper way to get the current hour
var hr = new Date().getHours();

// use if elseif
if(hr < 12) {
    alert("Good Morning");
} else if(hr < 18) {
    alert("Good Afternoon");
} else {
    alert("Good Evening");
}

// use var so we don't pollute
var msg = [
    "how are you",
    "whazzup",
    "how have you been",
    "how has your day been going",
    "hey y'all"
];

var random_num = Math.round(Math.random()*4);

alert(msg[random_num]); 
// proper way to get the current hour
var hr = new Date().getHours();

// use if elseif
if(hr < 12) {
    alert("Good Morning");
} else if(hr < 18) {
    alert("Good Afternoon");
} else {
    alert("Good Evening");
}

// use var so we don't pollute
var msg = [
    "how are you",
    "whazzup",
    "how have you been",
    "how has your day been going",
    "hey y'all"
];

var random_num = Math.round(Math.random()*4);

alert(msg[random_num]); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文