Last active
August 1, 2018 17:14
-
-
Save sevperez/ef3e47bf23482ab16a6b66801dec5d12 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function makeCalendar(name) { | |
var calendar = { | |
owner: name, | |
events: [], | |
}; | |
return { | |
addEvent: function(event, dateString) { | |
var eventInfo = { | |
event: event, | |
date: new Date(dateString), | |
}; | |
calendar.events.push(eventInfo); | |
calendar.events.sort(function(a, b) { | |
return a.date - b.date; | |
}); | |
}, | |
listEvents: function() { | |
if (calendar.events.length > 0) { | |
console.log(calendar.owner + "'s events are: "); | |
calendar.events.forEach(function(eventInfo) { | |
var dateStr = eventInfo.date.toLocaleDateString(); | |
var description = dateStr + ": " + eventInfo.event; | |
console.log(description); | |
}); | |
} else { | |
console.log(calendar.owner + " has no events."); | |
} | |
}, | |
}; | |
} | |
var babbageCalendar = makeCalendar("Charles Babbage"); | |
babbageCalendar.addEvent("Coffee with Ada.", "8/7/2018"); | |
babbageCalendar.addEvent("Difference Engine presentation.", "8/2/2018"); | |
babbageCalendar.listEvents(); | |
/* | |
Logs: | |
Charles Babbage's events are: | |
8/2/2018: Difference Engine presentation. | |
8/7/2018: Coffee with Ada. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment