0

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.

2
  • What is newArray()? If you're trying to instantiate an array, just use var pair = []; Commented Dec 18, 2012 at 8:07
  • I assume it is an array. In any case, as long as the first alert gets called, it should work. I see nothing wrong with it. See here: jsfiddle.net/Xhzfh is this really exactly the same code? Commented Dec 18, 2012 at 8:12

3 Answers 3

3

Change newArray() to new 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:

if (id[randomid] === null) {

with 3 = sign. Change it to just two:

if (id[randomid] == null) {

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.

Sign up to request clarification or add additional context in comments.

7 Comments

There will bi an issue with the j-- in the second loop too, but this should be the problem, I'm wondering though why the first alert fires, if newArray is undefined, it should throw an expception at line 0
Oh. Oooooooh. Welp, face, meet palm. :P
@Glutamat yes I noticed there is another error that will make freeze the browser inside the loop.
Yes, I actually checked if I declared my arrays correctly and somehow came to the conclusion I did. Changing it to new Array() fixed it, thanks! (still throws an exeption, but I guess i can continue to debug it now)
@HanletEscaño You're also right that the strict comparison there needed to be removed. Which is a bit odd, I only added that because a tutorial said that I should always use strict comparison when comparing to null values. Thank you for the help, kind sir.
|
1

In this piece of code:

if (id[randomid] === null){
    id[randomid] = pair[i];
    alert("ID " + randomid + "is " + id[randomid]);
}
else j--;

You can't mix the if/else shorthand:

if(something)
    //Do X
else
    //Do y

With it's bracket notation:

if(something){
    //Do X
} else {
    //Do y
}

So, put brackets around your else:

if (id[randomid] === null){
    id[randomid] = pair[i];
    alert("ID " + randomid + "is " + id[randomid]);
} else {
    j--;
}

Comments

0

The for loop should read:

for (var i=0; i<pairs.length; i++) {
   ...
}

pairs is being coerced into a numeric value (zero in this case), so the first comparison (i<pairs) will be the same as 0<0 which will never true.

:-(

6 Comments

It is, but you need to compare i to its length, not to the object itself (which is non-sensical).
that sort of comparison ($i < @array) is OK in Perl, but not in JavaScript.
also, assuming no other code outside your example, you still have a few other problems.
var pairs = 2; its definitely an Integer not an array
Ah, I see what you mean now. Oops :-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.