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?
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 (".").
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!)
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
}
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!
Bookmarks