0

I used this code and now the numbers 1-9 with zeros are last after 31. Example "28, 29, 30, 31, 01, 02,". what i want is to have it the regular way 01, 02, 03...09, 10, 11. how would i do this?

var everyDay = {};
for (im;im<=31;im++){
     t = (im < 10 ? '0' : '') + im
     everyDay[t] = (im < 10 ? '0' : '') + im;

}
Craig
  • 133
  • 1
  • 13

2 Answers2

1

It seems like the object properties are in lexicographical order (aa,ac,...,az). Have you tried an array instead?

var everyDay = [];
for (var im = 1; im <= 31; im++){
     everyDay.push((im < 10 ? '0' : '') + im);
}

However, it's hard to tell since you don't show how you use everyDay.

Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
Zeta
  • 103,620
  • 13
  • 194
  • 236
  • 1
    There is no telling in which way object properties are ordered. Don't build programs that rely on a particular order. http://stackoverflow.com/questions/280713/elements-order-in-a-for-in-loop – Tomalak Apr 23 '12 at 07:40
0

everyDay is a hash, which is not sorted by key. You will need to put the values in an array instead. Try this:

var days = []
for (im;im<=31;im++){
    t = (im < 10 ? '0' : '') + im
    days.push();
}
Gwyn Howell
  • 5,365
  • 2
  • 31
  • 48