Results 1 to 3 of 3

Thread: Please help with understanding string.length please

  1. #1
    Join Date
    Dec 2012

    Please help with understanding string.length please

    Hello this will be my first of many posts i'm sure because this seems like such a great site. Well anyway i'm sort of new to the scene, the web development scene that is but i really want to start
    learning html 5, so up to this point I have a really decent understanding of x/html, css and xml so now i'm trying to get a handle on javascript before I dwell into html 5. So I've been reading this book called "Eloquent Javascript" which supposedly is a good introduction to the language, but maybe its me but man I am having such a hard time learning this stuff. I am that kind of guy who wants to
    know how and why something works, instead of just memorizing the formula and using it when I need to, and so far this book doesn't do that for me at all. But anyway I'm on page 36 and and the
    book throws out an example of a function using the string and the math.round objects. Here is the example:

    function zeroPad(number, width) {
    var string = String(Math.round(number));
    while (string.length < width)
    string = "0" + string;
    return string;

    Ok now the book then goes on to explain that math.round is a function that rounds a number and the string is a function that converts its argument to a string, I get that but the part
    that is confusing to me is this section:

    while (string.length < width)
    string = "0" + string;

    The reason I am confused is that say for instance I use print(zeroPad(2, 4)); I obvuously will get 0002 but I don't see how because when I look at this (and ofcourse I now I'm wrong)
    while string.length (which is 1 because the number 2 is a string of 1 in my head ) is less then ( < ) width (which is 4) so moving along to string = "0" + string; must mean "02" as the answer.
    So whats really confusing to me is how and does the string function just suddenly decides that its going to return a string length of "4"? I know this was fairly long and I apologize but I would really
    appreciate your help ladies and gents. So far it has been a pain for me to say the least to learn javascript compared to the other languages I mentioned, but in a way for my benefit it feels like a goodthing

  2. #2
    Join Date
    Aug 2008
    As you probably understand, the length property of a string represents the length of the string, or in other words how many characters the string contains. When you say string = "0" + string; a new string object is created from the "0" + string part, and the new string also has a length property. That new string is then assigned to the 'string' variable. So since the string now has another character prepended to it the length property has increased by one after that statement.

    So to completely break down what happens in your function when saying print(zeroPad(2, 4)):

    First 2 is rounded and converted to string: "2"
    then, is string.length (1) less than 4? yes!
    execute loop, prepend "0" to "2" so we get "02"
    then, is string.length (2) less than 4? yes!
    execute loop, prepend "0" to "02" so we get "002"
    then, is string.length (3) less than 4? yes!
    execute loop, prepend "0" to "002" so we get "0002"
    then, is string.length (4) less than 4? no!
    skip the loop and return "0002"

    I hope that clarifies what happens.
    Last edited by ReFreezed; 12-03-2012 at 09:20 AM.

  3. #3
    Join Date
    Dec 2012
    Refreezed thank you so much for explaining that to me so eloquently. I fully understand it now, now I can move on.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center