www.webdeveloper.com
Results 1 to 6 of 6

Thread: Array intermittently missing elements

  1. #1
    Join Date
    May 2012
    Location
    Portland, OR
    Posts
    3

    Array intermittently missing elements

    Hi everyone:

    I am learning JavaScript and have run into an unsual situation that's been driving me crazy for the last few days... google and a couple refrence books I bought so far have been unfruitful, but I'm sure I'm missing something simple.

    I have made a dice game, similar to yahtzee aka yatzy; pretty much everything works (algorthems that check to see if you have a full house or a small straight, etc..) but only about 3 out of 4 times. I've isolated the issue down to this simple function which determines what dice the player has and inserts those values into an array. The dice are displayed as .gif images with the names 1.gif, 2.gif... 6.gif. (each die is displayed in a <img> with an id from 0-5) Here is the function:

    function getTheDice(){
    var theDice = new Array();
    for (i=0;i<5;i++){
    var path = document.getElementById(i).src;
    theDice[i] = path.substring(path.lastIndexOf('/')+ 1, path.lastIndexOf('.'));
    }
    return theDice
    }

    so, if you have a 1, a 2, a 3, a 4, and a 5 showing, then it would return 1,2,3,4,5. Many other functions depend on this function, obiviously... and it works most of the time but sometimes it would return 0,2,3,4,5 or 1,2,0,4,5, etc... from what I can tell there doesn't seem to be any pattern... sometimes it's the first element, sometimes it the 3rd could be any, sometimes it's a 1 that is a 0 sometimes it a 6... and all in between... to me it really makes no sense at all how this could be happening intermittently like this, I think I'm minsunderstaning something really basic and low-level about the way JS is working here... any ideas?

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    Code:
    function getTheDice(){
     var theDice = new Array();
     for (i=0;i<5;i++){
     var path = document.getElementById(i).src;
     theDice[i] = path.substring(path.lastIndexOf('/')+ 1, path.lastIndexOf('.'));
     }
     return theDice
     }
    1. the very first suspicious thing i see here is the id attribute format used in your code

    ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
    (http://stackoverflow.com/questions/7...ribute-in-html)

    2. the second thing i would do another way is this line:

    theDice[i] = path.substring(path.lastIndexOf('/')+ 1, path.lastIndexOf('.'));

    i would write it like this:

    Code:
    var n = new Number(path.substring(path.lastIndexOf('/')+ 1, path.lastIndexOf('.')));
    theDice.push(n);
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  3. #3
    Join Date
    May 2012
    Location
    Portland, OR
    Posts
    3

    Thanks!

    Thank you for your reply, I didn't know about the rules/conventions for ID tags, and I hadn't thought of push but that does seem to be better.

    Unfortunately, even after implementing your suggestions, my code is behaving the same way... working most of the time, but still randomly/intermittently returning a '0' where there shouldn't be. I even rolled the same thing twice (4,4,4,4,4) and the first time it worked, the very next time it returned 4,4,4,0,4.

    I'm going to keep looking through the rest of my code (like the functions which choose the src of the img randomly, etc) and see if I can find any other areas that might be causing this error... and if anyone else thinks of anything, please let me know

    Thanks again for your response I do appreciate it (I learned something new about JavaScript, after all!)

  4. #4
    Join Date
    Aug 2010
    Location
    San Diego
    Posts
    242
    Very strange. A quick cheap debug would be to use this:
    Code:
    function getTheDice(){
      var theDice = new Array();
      for (i=0;i<5;i++){
        var path = document.getElementById(i).src;
        var n = path.substring(path.lastIndexOf('/')+ 1, path.lastIndexOf('.'));
        if (n==0) alert ( "0 found in " + path );
        theDice[i] = n;
      }
      return theDice
    }
    See if the contents of "path" tells you anything.

  5. #5
    Join Date
    May 2012
    Location
    Portland, OR
    Posts
    3

    that'll do it!

    Thanks handcraftedweb! After using your method I found that every once in a while the file path would be "...img/0.gif" which is very odd because I have no img with the name 0.gif, and yet even when the src was .../img/0.gif image were still displaying correctly on the page (and a different image each time). But, I was able to modify the function that randomized the images to avoid any zeros being included, and now it works like a charm! Thank you again!

  6. #6
    Join Date
    Nov 2010
    Posts
    1,083
    Last edited by xelawho; 05-03-2012 at 11:38 PM. Reason: already resolved

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



Recent Articles