I am trying out javascript for (more-or-less) the first time and find myself completely baffled by the following .js script.
var pair = newArray();
var id = newArray();
var pairs = 2;
function newGame(){
var randomid = 0;
alert("newGame() called!");
// Sets a specific part of the image sprite to each pair[].
for (var i=0 ;i < pairs; i++){
alert("For loop started!");
pair[i] = "url(Cardfront.jpg) -"+100 * Math.floor((Math.random()*13)+0)+"px -"+200 * Math.floor((Math.random()*4)+0)+"px";
// For every pair, assigns a part of the image sprite to two id[]-s.
alert("Pair " + i + "is " + pair[i]);
for(var j=0; j < 2; j++) {
//the range of possible id-s the total number of cards - double the amount of pairs.
randomid = Math.floor((Math.random()*pairs*2)+0);
if (id[randomid] === null){
id[randomid] = pair[i];
alert("ID " + randomid + "is " + id[randomid]);
}
else j--;
}
}
alert("This is called after the for loop!");
}
When I call newGame() through a button, I receive the “newGame() called!” and then “This is called after the for loop!” alerts, then nothing.
I’ve spent a while googling and poking around trying to figure this out, but I’m at the end of my wits, it seems.
Change
newArray()tonew Array()I believe that is what is causing your error sir.Good luck!
EDIT: To fix the other error I found you have the following:
with 3
=sign. Change it to just two:and it should work the way you expect it to.
Unless you are really trying to use the strict comparisson operator, then there is something else bugging your code.