I am creating a javascript function to build the html for a calendar (in a <table>) for the current month. One of the parameters of the function is whether to write out the days of the week or use their initials. The full names and initials are both contained in arrays. The function will loop over one of the arrays to build the table cells that contain the days of the week. What is the best way to pick which array to loop over? Or should I be constructing this code in an entirely different way?
Code to illustrate my question:
buildCalendar(useFullNames){
var fullNames = ['Sunday', 'Monday'], // etc.
initials = ['S', 'M'],
calString = '<tr>';
if(useFullNames) {
// use fullNames array in the loop
}
else {
// use initials array in loop
}
for(i=0; i<7; i++)
{
// Need to loop over the array picked above
calString += '<td>' + relevantArray[i] + '</td>';
}
calString += '</tr>';
}
1 Answer