If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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:

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?

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!)

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!