简体   繁体   English

我在运行以下代码时遇到问题,我想打印 msg

[英]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试试下面的代码

index.html索引.html

<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)将小时数变成012

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.

相关问题 我在下面有此代码,我希望控制台显示文本 - I have this code below and I want the console to display the text 我希望下面的代码以 10 到 0 的反向方式打印 - i want below code to print for reverse way like 10 to 0 我想在提示中按下 Esc 按钮时提醒一个值,但我不知道...(下面的代码) - I want to alert a value when I press the Esc button in prompt, but I have no idea... (code below) 我必须在jquery中阅读childs div的坐标,我想按x坐标进行排序,我的代码如下 - I have to read Coordinates of childs div in jquery and i want to sort out by x coordinates my code is below 我在下面有这个代码我希望计数器在发送消息后重置为 0 - I have this code below i want the counter to reset to 0 after the message is sent 我想在每个按钮单击事件上加载多个图表。 - I want to load multiple charts on every button click event.I have used below google code 我想从下面的代码超链接地址 - i want hyperlink address from below code 我想在下面的代码中添加单引号('') - I want to add single quotes in the below code ('') 我想将以下代码解构为预期的甲酸盐 - i want to destructure the below code to expected formate 我在运行 reactjs 应用程序时遇到问题。下面给出的是错误 - I am facing a problem while running reactjs application.Given below is the error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM