I'm learning Javascript for the first time, and using online tutorials from w3resource.com to help me understand the basics.
I see something happening in a tutorial that I don't understand, but I have no way to know whether the tutorial is misleading me or there's something I'm missing.
Basically, it's a small program to print out the date and time. I'm being shown the HTML and the JS so that I can see how it works. Based on what I've read about JS, the HTML requires a "script" tag so that it knows to incorporate the JS. However I don't see that tag being used in the tutorial. Does that mean the tutorial is misleading me as to what is proper protocol, or is there someway to embed JS in HTML without the "script" tag?
Here is what I am being shown:
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript current day and time</title>
</head>
<body>
</body>
</html>
Javascript:
var today = new Date();
var day = today.getDay();
var daylist = ["Sunday","Monday","Tuesday","Wednesday ","Thursday","Friday","Saturday"];
console.log("Today is : " + daylist[day] + ".");
var hour = today.getHours();
var minute = today.getMinutes();
var second = today.getSeconds();
var prepand = (hour >= 12)? " PM ":" AM ";
hour = (hour >= 12)? hour - 12: hour;
if (hour===0 && prepand===' PM ')
{
if (minute===0 && second===0)
{
hour=12;
prepand=' Noon';
}
else
{
hour=12;
prepand=' PM';
}
}
if (hour===0 && prepand===' AM ')
{
if (minute===0 && second===0)
{
hour=12;
prepand=' Midnight';
}
else
{
hour=12;
prepand=' AM';
}
}
console.log("Current Time : "+hour + prepand + " : " + minute + " : " + second);
This is the tutorial I am using: http://www.w3resource.com/javascript-exercises/javascript-basic-exercise-1.php
If anyone could explain to me how this JS is being embedded in the HTML without the tag I would appreciate it. I am going to be tested on this in person next week and I will be asked to write code from scratch, so I can't afford misunderstandings like this.