Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jul 2012
    Location
    Mexico
    Posts
    45
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Strange behavior on a script

    Please can you help here, but I'm thinking this is a specific problem with my website, not a syntax or coding error. I have this script:

    Code:
    <script type="text/javascript">
    var losTiempos= new Array();
    losTiempos[0]="menos de 10 min";
    losTiempos[1]="1/2 hora";
    losTiempos[2]="1 hora";
    losTiempos[3]="1 hora y media";
    losTiempos[4]="2 horas";
    losTiempos[5]="2 horas y media";
    losTiempos[6]="3 horas";
    losTiempos[7]="3 horas y media";
    losTiempos[8]="4 horas";
    losTiempos[9]="4 horas y media";
    losTiempos[10]="5 horas";
    for (item in losTiempos){
    alert(item+" - "+losTiempos[item]);
    }
    </script>
    In every cell of losTiempos there must be each one of the strings above, but instead it assigns each character of the last string (in this case, "5 horas") to every cell, and the number of cells is equal to the number of characters in that string. If I remove the last line, is the new last line that fills the cells. This does not happens with other arrays in the same place.

    So the result of the alerts is this:

    0 - 5
    1 -
    2 - h
    3 - o
    4 - r
    5 - a
    6 - s

    I've tested the exact same script in the Try-It-Yourself editor of W3Schools.com, and it works as expected, so what could be wrong here?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,650 Times in 4,612 Posts
    Doesn't happen for me.

    I changed your alert() to document.write() and I get this output:
    Code:
    0 - menos de 10 min
    1 - 1/2 hora
    2 - 1 hora
    3 - 1 hora y media
    4 - 2 horas
    5 - 2 horas y media
    6 - 3 horas
    7 - 3 horas y media
    8 - 4 horas
    9 - 4 horas y media
    10 - 5 horas
    What browser are you using?

    I should note that normally we would not use for (item in losTiempos) for an indexed array like that.

    And the array can be created more simply, as well:
    Code:
    <script type="text/javascript">
    var losTiempos= [
        "menos de 10 min",
        "1/2 hora",
        "1 hora",
        "1 hora y media",
        "2 horas",
        "2 horas y media",
        "3 horas",
        "3 horas y media",
        "4 horas",
        "4 horas y media",
        "5 horas"
      ];
    for (var i = 0; i < losTiempos.length; ++i )
    {
        document.write(i + " - "+losTiempos[i] + "<br/>");
    }
    </script>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    juliushg (01-29-2013)

  • #3
    New Coder
    Join Date
    Jul 2012
    Location
    Mexico
    Posts
    45
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thanks! It worked ok when creating the array the way you put it above. It must be some problem with the Wordpress background, now it's ok. Anyway, why the "item in array" is not recommended?

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,650 Times in 4,612 Posts
    I didn't say it's not recommended. It's a tiny bit more overhead and isn't needed for an array where the elements occur in numerical sequence. So, as I said, we *normally* use an index in a loop. But clearly "for...in..." it is needed when the elements aren't numbered.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New Coder
    Join Date
    Jul 2012
    Location
    Mexico
    Posts
    45
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Ah ok, I got it, thanks.


  •  

    Posting Permissions

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