Question: Why isn't my code working? 1. Add the other functions that are needed for this application to the library file. To do that, move the
Why isn't my code working?
1. Add the other functions that are needed for this application to the library file. To do that, move the code from the main JavaScript file to the library and adjust as needed. When youre through, the library should include separate functions for (1) making sure both entries have been made, (2) testing the validity of just the date entry, (3) calculating the number of days until the event, and (4) displaying the number of days until the event.
Modify the countdown.js file so it uses the functions in the library to get the correct results.
"use strict";
var $ = function(id) { return document.getElementById(id); };
var clearMessage = function(messageNode) { messageNode.nodeValue = " "; };
var calculateDays = function() { var event = $("event").value; var dt = $("date").value; var message = $("message").firstChild; var date, days, today, oneDay; var convertDate = 0; var displayDayDifference; message.nodeValue = " "; // if no error messages, calculate and display days until event if (message.nodeValue === " ") {
//calculate days today = new Date(); days = Math.ceil(days);
} };
var isFormFilled = function(event,dt,message) { if (event.length === 0 || dt.length === 0) { message.nodeValue = "Please enter both a name and a date."; return false; } else{ return true; } }; var isValidDate = function(dt,message) { if (isValidFormat(dt,message)) { var date = new Date(dt); console.log("1"); if (date === "Invalid Date") { message.nodeValue = "Please enter the date in MM/DD/YYYY format."; console.log("2"); return false; } } else { return true; } }; var isValidFormat = function (dt, message) { if (dt.indexOf("/") === -1) { message.nodeValue = "Please enter the date in MM/DD/YYYY format."; return false; } var year = dt.substring(dt.length - 4); if (isNaN(year)) { message.nodeValue = "Please enter the date in MM/DD/YYYY format."; return false; } else { return true; }
var convertDate = function(messageNode) { return (messageNode.nodeValue === " ") ? true: false; };
if (message.nodeValue === " ") {
//calculate days var date; var today = new Date(); var oneDay = 24*60*60*1000; // hours * minutes * seconds * milliseconds var days = ( date.getTime() - today.getTime() ) / oneDay; days = Math.ceil(days); //create and display message if (days === 0) { message.nodeValue = "Hooray! Today is " + event + "!"; } if (days < 0) { event = event.substring(0,1).toUpperCase() + event.substring(1); // capitalize event message.nodeValue = event + " happened " + Math.abs(days) + " day(s) ago."; } if (days > 0) { message.nodeValue = days + " day(s) until " + event + "!"; } return days; } };
window.onload = function() { $("countdown").onclick = calculateDays; $("event").focus(); };
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
