[英]I have a problem while running the below code, I want to print msg
I am trying to run the code but the error like "Object error comes"我正在尝试运行代码,但出现“对象错误”之类的错误
var today= new Date(); var hourNow = today.getHours(); if (hourNow > 18) { console.log("GoodEvening"); } else if (hourNow > 12) { console.log("GoodAfternoon"); } else if (hourNow > 0) { console.log("GoodMorning"); }
try below code试试下面的代码
<html>
<head>
<title>My awesome webpage</title>
</head>
<body>
<h1>This site is awesome!</h1>
<p>And I coded it from scratch.</p>
<div id="disply-date"></div>
<script>
var today= new Date();
var hourNow = today.getHours();
if (hourNow > 18) {
console.log("GoodEvening");
document.getElementById("disply-date").innerHTML = "GoodEvening";
}
else if (hourNow > 12) {
console.log("GoodAfternoon");
document.getElementById("disply-date").innerHTML = "GoodAfternoon";
}
else if (hourNow > 0) {
console.log("GoodMorning");
document.getElementById("disply-date").innerHTML = "GoodMorning";
}
</scirpt>
</body>
</html>
console.log(["Good Morning", "Good Afternoon", "Good Evening"][~~(new Date().getHours() / 8)]);
Code Golf!代码高尔夫!
How this works:这是如何工作的:
You don't need to create a new Date object and stash it in a variable if you are just getting the hours.如果您只是获得时间,则无需创建新的 Date object 并将其存储在变量中。 So you can do:
new Date().getHours()
.所以你可以这样做:
new Date().getHours()
。
It also turns out, that you can place your messages in an Array and select the message you want like: var arr = ["Good Morning", "Good Afternoon", "Good Evening"]; console.log(arr[2]);
事实证明,您可以将您的消息放在一个数组中,select 您想要的消息如下:
var arr = ["Good Morning", "Good Afternoon", "Good Evening"]; console.log(arr[2]);
var arr = ["Good Morning", "Good Afternoon", "Good Evening"]; console.log(arr[2]);
. . But you don't need to stash the array in a variable to get the message.
但是您无需将数组存储在变量中即可获取消息。 You can do this:
["Good Morning", "Good Afternoon", "Good Evening"][2]
.你可以这样做:
["Good Morning", "Good Afternoon", "Good Evening"][2]
。 Sure, it looks a little weird, but it's saying, "Hey, here's an array of strings, get me the one at index 2."当然,它看起来有点奇怪,但它是在说,“嘿,这是一个字符串数组,给我在索引 2 处的那个。”
So how to get the hours and turn it into 0, 1 or 2 (our indexes)?那么如何获取小时数并将其转换为 0、1 或 2(我们的索引)? Well, take the hours and divide by 8. That will get you close.
好吧,把时间除以 8。那会让你接近。 Say it's 7:30 am.
假设现在是早上 7 点 30 分。 You take 7 / 8 and get 0.875.
你取 7 / 8 得到 0.875。 That isn't an index.
那不是索引。 We need to round down.
我们需要四舍五入。 So we can do
Math.floor(7/8)
and we get 0.所以我们可以做
Math.floor(7/8)
得到 0。
We can feed THAT ( 0
) to our array and get Good Morning
.我们可以将 THAT(
0
) 提供给我们的数组并获得Good Morning
。 Cool.凉爽的。
Math.floor
is too many characters to type and I'm lazy. Math.floor
有太多字符无法输入,我很懒。 There's a Javascript hack using bitwise operators explained here .有一个 Javascript hack 使用这里解释的按位运算符。 Basically
~~
does a Math.floor
.基本上
~~
做一个Math.floor
。 So we can cheat and do ~~(new Date().getHours() / 8)
to turn the hours into 0
, 1
, or 2
.所以我们可以作弊并做
~~(new Date().getHours() / 8)
将小时数变成0
、 1
或2
。
Take that result, get our string out of our array and smash the entire thing into console.log.拿这个结果,把我们的字符串从我们的数组中取出,然后把整个东西粉碎到 console.log 中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.