Jump to content

Splice method query


afish674

Recommended Posts

I have an array that shows culminatively the number of days in a year. Jan the first starts with 0 and then Feb 1st starts with 31 etc.

var months = new Array();  months[0]=0;  months[1]=31;  months[2]=59;  months[3]=90;  months[4]=120;  months[5]=151;  months[6]=181;  months[7]=212;  months[8]=243;  months[9]=273;  months[10]=304;  months[11]=334;

I want to change this for a leap year so that everything after Feb (array element 2 onwards) has +1 added to their values. I could do this via the splice method:

months.splice(2,1,60);months.splice(3,1,91)//etc..

But this seems long winded, is there a quicker way? Like putting "months.splice(2,10,value+1);" or something similar? Thanks!

Link to comment
Share on other sites

Loop through all the months and add 1 to their values:

if(isLeapYear) {    for(i = 2; i < months.length; i++) {	    months[i]++;    }}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...