I am currently learning Javascript and wanted to ask how can I run a while loop through several if statements so that I would get the 12 months displayed in a list, I think I am suppose to use a counter something like
while(monthName < 10){
But I am not sure how to incorporate it into my function:
window.onload = function() {
document.getElementById("months").innerHTML = getMonth(0);
};
function getMonth(month) {
var monthName;
if (month == 0) {
monthName = "January";
}
if (month == 1) {
monthName = "February";
}
if (month == 2) {
monthName = "March";
}
if (month == 3) {
monthName = "April";
}
if (month == 4) {
monthName = "May";
}
if (month == 5) {
monthName = "June";
}
if (month == 6) {
monthName = "July";
}
if (month == 7) {
monthName = "August";
}
if (month == 8) {
monthName = "September";
}
if (month == 9) {
monthName = "October";
}
if (month == 10) {
monthName = "November";
}
if (month == 11) {
monthName = "December";
}
return monthName;
}
http://jsfiddle.net/priswiz/rUpsb/
I understand there are more effective ways of doing this, like using an Array, but asking for while loop/counter for educational purposes.
Thank you :)
switchstatement.