Advertisement
MoronPipllyd

Untitled

Sep 12th, 2019
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.90 KB | None | 0 0
  1. <!DOCTYPE HTML>
  2. <html>
  3.   <head>
  4.     <title>Greeting Demo</title>
  5.   </head>
  6.   <body>
  7.     <!-- TODO You should probably move the contents of this script tag into
  8.      your .js file and link it up top in the <head> !-->
  9.     <script>
  10.  
  11.       /**
  12.         * Util function to return either Morning, Afternoon or Evening
  13.         * depending on time
  14.       */
  15.       function getTimeOfDay() {
  16.         // Create a new date object for right now
  17.         let now = new Date();
  18.         // Retrieve the current hour
  19.         let hour = now.getHours();
  20.  
  21.         // If it's between 0:00 and 12, it's morning
  22.         if(hour < 12 && hour >= 0) {
  23.           return "Morning";
  24.         }
  25.  
  26.         // If it's between 12:00 and 17:00 it's Afternoon
  27.         if(hour >= 12 && hour < 17) {
  28.          return "Afternoon";
  29.         }
  30.  
  31.         // If it's between 17:00 and 0:00 it's evening
  32.         if(hour >= 17) {
  33.           return "Evening";
  34.         }
  35.       }
  36.  
  37.       /**
  38.         * Called when the input form is submitted to retrieve the
  39.         * user's name, get the time of day and print the greeting
  40.       */
  41.       function onSubmit() {
  42.         // Get the inputted name from the form and assign to variable
  43.         let name = document.getElementById("name").value;
  44.         // Use utility function to get the time of day
  45.         let timeOfDay = getTimeOfDay();
  46.         // Concatinate both variables and display to user
  47.         // TODO: Maybe replace the Alert with some actual HTML content that looks
  48.         // nice (https://www.w3schools.com/jsref/prop_html_innerhtml.asp)
  49.         alert("Good " + timeOfDay + ", " + name);
  50.       }
  51.     </script>
  52.  
  53.     <!-- Notice the id attribute of the below input tag, this used in the
  54.    above javascript to get the contents of the input -->
  55.     <input id="name" type="text" placeholder="Enter your Name"></input>
  56.     <button onclick="onSubmit()">Greet</button>
  57.   </body>
  58. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement