This needs be written in javascript. All i need help with is creating the variab
ID: 3871550 • Letter: T
Question
This needs be written in javascript. All i need help with is creating the variables and the functions to figure out the solution. The instructions are below.
Create a function that starts with a date and a number of days to add to date
in order to generate an end date.
Create an IIFE
(function () {
// logic here
})();
/* Need to know
- how many days in each month
- is it a leap year
- Option: keep leap years in array
- Option: base year and figure learp years from that
- Option: year divisible by 4 but not 100(see function below)
function leapYear(year)
{
return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}
Step 1: Get the day of the date
Step 2: Add number of days value
Step 3: Compare new balue with end of month
Step 4: Keep if before or on the last day of month
Step 5: Get number of days past last day of month
Step 5a: Increment month (check for Dec)
Step 5b: Set month to Jan and increment year if Dec
// Create a variable called thisDate using explict year (2017), month (September), and day (26) - OCT 11
var startDate = new Date(2017, 08, 26);
var daysToAdd = 15;
Explanation / Answer
function myFunction() {
var year = 2007;
var month = 11;
var day = 5423;
var startDate = new Date(year, month, day);
document.getElementById("demo1").innerHTML = startDate;
var daysToAdd = 6;
var day_in_months = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
while(daysToAdd >= 366){
if(leapYear(year+1)){
daysToAdd -= 366;
}else{
daysToAdd -= 365;
}
year +=1;
}
var total_days = day + daysToAdd;
while( (leapYear(year) && month==1 && total_days >= 29) ||
total_days >= day_in_months[month]){
if(leapYear(year) && month == 1){
total_days -= 29;
month += 1;
}
else{
total_days -= day_in_months[month];
if(month == 12){
month = 1;
year +=1;
}else{
month +=1;
}
}
}
var endDate = new Date(year, month, total_days);
return endDate;
}
function leapYear(year)
{
return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.