0

I am having some trouble figuring this out. I would like to loop through all the months once, instead of twelve times as my code is doing so now. Can anyone help, also is there a more effective way of looping this funciton?

Thanks!

    window.onload = function() {
    getMonth();
};

 function getMonth()
{
var x="",i=0;
var month=Array();
month[0]="January";
month[1]="February";
month[2]="March";
month[3]="April";
month[4]="May";
month[5]="June";
month[6]="July";
month[7]="August";
month[8]="September";
month[9]="October";
month[10]="November";
month[11]="December";

   while (i<12)
      {
      x=x  + month + "<br/>";
       i++;
       }
    document.getElementById("months").innerHTML=x;
    }

here is a link to my jsfiddle demo http://jsfiddle.net/priswiz/LqEE6/

2
  • 1
    You are just missing month[i] fiddle Commented Mar 15, 2013 at 20:45
  • 2
    Here's a updated fiddle based on @Sushil answer, jsfiddle.net/knoxzin1/LqEE6/3 Commented Mar 15, 2013 at 20:51

2 Answers 2

6

Simply use Array .join method.

var x = month.join("<br />");

your code will work if you use x=x + month[i] + "<br/>";

Sign up to request clarification or add additional context in comments.

Comments

0

I assume you are trying to access the array by index

while (i<12)
{
  x=x  + month[i] + "<br/>";
  i++;
}

Currently, you are printing the value of month.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.