-
-
Save potatowave/4e86438ac95cfe85ce7e7ad928ddfa3c to your computer and use it in GitHub Desktop.
W1D2 - Debugging incorrect code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var date = process.argv[2]; | |
if (!date) { | |
console.log("Please provide a date in the format YYYY/MM/DD"); | |
} else { | |
calculateDayInYear(date); | |
} | |
function calculateDayInYear(date) { | |
var splitDate = date.split('/'); | |
var year = Number(splitDate[0]); | |
var month = Number(splitDate[1]); | |
var day = Number(splitDate[2]); | |
console.log(year + " " + month + " " + day) | |
var febDays = daysInFeb(year); | |
var DAYS_IN_MONTH = [31, febDays, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; | |
if (year && validMonth(month) && validDay(month, day)) { | |
console.log(date, "is day", calculateDayNumber(month, day), "of the year", year); | |
} else { | |
console.log("Invalid date"); | |
} | |
function validMonth(month) { | |
if (month >= 1 && month <= 12) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
function validDay(month, day) { | |
//return day && day >= 1 && day < DAYS_IN_MONTH[month - 1]; | |
if ((day >= 1) && (day <= 31)) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
function calculateDayNumber(month, day) { | |
var dayOfYear = 1; | |
for (var i = 1; i < month; i++) { | |
dayOfYear += DAYS_IN_MONTH[i - 1]; | |
} | |
return dayOfYear; | |
} | |
function daysInFeb(year) { | |
return 28; | |
} | |
function isLeapYear(year) { | |
return isMultiple(year, 400) || !isMultiple(year, 100) && isMultiple(year, 4); | |
} | |
} | |
function isMultiple(numerator, denominator) { | |
return numerator % denominator === 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment