-2

I have this piece of code causing me grief. I need it to change the i variable to 01,02,03.....10,11,12 preferably on this line so as not to disrupt everything which runs off it. I've been messing around trying to do it elsewhere in the function and even the other end, but I keep breaking it. Really if I could just fix it on this line everything else would be fine. Please assist

for (var i=1; i<32; i++)
options[i]=new Option(i, i+0)

//I need the i variable for the rest of the function, it gets changed and outputted later

Kilisi
  • 402
  • 11
  • 33
  • 1
    That line of code doesn't "output" anything. – Pointy Jul 31 '15 at 22:46
  • this is an incomplete for loop – FutoRicky Jul 31 '15 at 22:46
  • what do you mean preferable on this line? – Geno Diaz Jul 31 '15 at 22:46
  • should be something like ```for(var i=1; i<32;i++){ console.log(i); } ``` – FutoRicky Jul 31 '15 at 22:47
  • possible duplicate of [How can I create a Zerofilled value using JavaScript?](http://stackoverflow.com/questions/1267283/how-can-i-create-a-zerofilled-value-using-javascript) – adamdc78 Jul 31 '15 at 22:48
  • I already read the zero filled one...I couldn't work out how to use it, no need to downvote me, if I knew how to do it, I'd have already done it, thats why I asked for the assist. I don't want it to output anything at this point, I want it to prepend a zero to the number if it's 1-9 – Kilisi Jul 31 '15 at 23:28
  • 1
    The padding is a number formatting operation and not done until you're outputting it. Its integer value is the same no matter how many zeros preceed it. You could convert it to a string and pad to two digits using the loops below, but it's still a duplicate question (there's a question closer to yours which was marked a duplicate of the one I flagged). [pad-a-number-with-leading-zeroes-in-javascript](http://stackoverflow.com/questions/10073699/pad-a-number-with-leading-zeros-in-javascript) – adamdc78 Jul 31 '15 at 23:30
  • @adamdc78 Ok, that makes sense to me now, so that would be done on the second line of my edited code rather than the first? – Kilisi Jul 31 '15 at 23:36
  • Yes; something like `options[i]=new Option(i, pad(i));` where pad(i) would prepend `0` as in the answers below. I'm not sure what you're doing with Option, it may make more sense to do the padding in the getter for that value. – adamdc78 Jul 31 '15 at 23:39
  • pad(i) ? I need a separate function? – Kilisi Jul 31 '15 at 23:48
  • 1
    You could do `(i < 10 ? '0' : '') + i` instead. – adamdc78 Jul 31 '15 at 23:52
  • I used options[i]=new Option(((i < 10 ? '0' : '') + i), i+0) which solves the problem and makes a new one, but I can work it out from here, cheers – Kilisi Aug 01 '15 at 00:04

2 Answers2

2

Is something like this what you're looking for?

for (var i=1; i<32; i++)
    i < 10 ? document.write('0' + i + '<br />') : document.write(i + '<br />');   
mcon
  • 679
  • 6
  • 22
  • this is great, but I don't know how to put it in, I'll edit my code and add the next line to show what I mean, I'm very new to js – Kilisi Jul 31 '15 at 23:23
  • 1
    @Kilisi I've been messing around with the info you updated with, and I can't push a value like `01` or `02` as the value of the `Option` object. They always come out as a 1, 2, 3, etc value. I can post some code showing that if you'd like to see it. – mcon Jul 31 '15 at 23:58
  • sure, post please, I'm learning a lot here, this is what I got working with some help from comments on the second line options[i]=new Option(((i < 10 ? '0' : '') + i), i+0) which is very close to your answer – Kilisi Aug 01 '15 at 00:08
  • 1
    @Kilisi I think I wrote it wrong the first time by accident because this new [fiddle](http://jsfiddle.net/wgrugfzx/3/) is logging valid options elements. Check it out and let me know if it's something you're looking for. – mcon Aug 01 '15 at 00:22
  • can't see the difference in your code, the first snippet was working, but it helped me understand the output issue as well, cheers – Kilisi Aug 01 '15 at 00:36
  • @Kilisi The only real difference is creating array elements in every iteration of the loop rather than outputting it to the page. Glad you were able to figure it out, though! – mcon Aug 01 '15 at 00:47
1

Something like this should work

for (var i=1; i<32; i++) {
    var number = i;
    if (i<10) {
         number = 0 + '' + i;
    }
    console.log(number);
}

You'll get the output into the browsers console, simply change this to wherever you want the text to be.

baao
  • 71,625
  • 17
  • 143
  • 203
  • this looks useable as well, I have changed my question slightly though, I don't need it to output anything yet, just change the variable, I worded the question badly through inexperience – Kilisi Jul 31 '15 at 23:31