Hello, I'm still getting the hang of JavaScript - just a few days into learning it. I can't figure out why this function I'm calling isn't functioning as expected.
Here's the content of my HTML page:
<!doctype html>
<html>
<head>
<title>Document</title>
</head>
<body>
<h1>Practice</h1>
<button id="btn" onclick="printDate()">Print Date</button>
<p id="Day"></p>
<button onclick="printTime()"> Show Clock </btn>
</br>
<p id="Time"></p>
<script src="Clock.js"></script>
</body>
</html>
In clock.js, you'll find the following code:
// This is where the function is defined
function printDate() {
// Retrieving values from the "Date" object using different methods
var date = new Date();
var day = date.getDay();
var dateNum = date.getDate();
var month = date.getMonth();
var year = date.getYear();
// Adjusting for JS year format
year += 1900
// Changing the numerical value of "day" to the actual day name string
switch(day){
case 0: day = "Sunday"
break;
case 1: day = "Monday"
break;
case 2: day = "Tuesday"
break;
case 3: day = "Wednesday"
break;
case 4: day = "Thursday"
break;
case 5: day = "Friday"
break;
case 6: day = "Saturday"
break;
}
// Changing the numerical value of "month" to the actual month name string
switch(month){
case 0: month = "January"
break;
case 1: month = "February"
break;
case 2: month = "March"
break;
case 3: month = "April"
break;
case 4: month = "May"
break;
case 5: month = "June"
break;
case 6: month = "July"
break;
case 7: month = "August"
break;
case 8: month = "September"
break;
case 9: month = "October"
break;
case 10: month = "November"
break;
case 11: month = "December"
break;
}
// Displaying the values in the p tag with ID "Day"
document.getElementById("Day").innerHTML = "Today is: "+day+" the "+dateNum+" of "+month+", "+year
}
function printTime() {
document.getElementById("Time").innerHTML = "Current Time"
}
I apologize for all the comments; they're helping me learn.
Now, onto the issue - when I try to call printTime() in the HTML document, it doesn't work and doesn't replace the innerHTML with "Current Time." However, the other function works perfectly fine. What could be causing this discrepancy? Why does the first function work but not the second?
Any assistance would be greatly appreciated!