0

I have a javascript function that loos like this :

var usedNums= new Array(76);
function setSquare(thisSquare)
{

    var currSquare = "square"+ thisSquare;
    var colPlace =  new Array(0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4);
    var colBasis = colPlace[thisSquare] * 15;

    var newNum;
    do{
      newNum = colBasis + getNewNum() + 1;

    }while(usedNums[newNum]); //UnCaught TypeError here

    usedNums[newNum] = true;
    document.getElementById(currSquare).innerHTML = newNum;
}

The error says:

Can not read property '5' of undefined.

I have checked using console.log statement and all the variables above are getting expected values .

I understand what Type Error is but not sure where is it breaking.

**Edit:**Here is the complete Script:

window.onload = new newCard;
var usedNums= new Array(76);

function newCard()
{
  if(document.getElementById){
      for(var i =0; i<24;i++)
      { 
          console.log("Value of I is " + i);
          setSquare(i);

      }
  }else{

   alert("Sorry, your browser does not support this script");
  }
}

function setSquare(thisSquare)
{
    console.log("thissquare" + thisSquare);
    var currSquare = "square"+ thisSquare;
    console.log("currsquare" + currSquare);
    var colPlace =  new Array(0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4);
    console.log("colplace" + colPlace);
    var colBasis = colPlace[thisSquare] * 15;
    console.log("colbasis"+ colBasis);
    var newNum;
    do{
      newNum = colBasis + getNewNum() + 1;
      console.log("new nUM" + newNum);
    }while(usedNums[newNum]);

    usedNums[newNum] = true;
    document.getElementById(currSquare).innerHTML = newNum;
}

function getNewNum()
{
var a = Math.floor(Math.random() * 15);
console.log("random number" + a);
return a;
}
11
  • 3
    where is the definition of usedNums, because it seems that it's not defined? Commented Apr 17, 2015 at 14:50
  • @Vasilev: It is outside of function.Updated the question. Commented Apr 17, 2015 at 14:53
  • 1
    Uncaught ReferenceError: getNewNum is not defined — Please make sure your provide code in the question that actually demonstrates the problem: sscce.org Commented Apr 17, 2015 at 14:57
  • @sscce.org: Error says UnCaught Type Error only.Added getNewNum definition in the question. Commented Apr 17, 2015 at 14:59
  • Please provide the code for getNewNum(). Providing an example of how you're using this function would be helpful as well. Commented Apr 17, 2015 at 14:59

1 Answer 1

1

This line:

window.onload = new newCard;

is:

  1. Invoking newCard as a constructor (which it isn't)
  2. Assigning the object created from it to onload

Since you are invoking newCard there, it runs before you reach the next line:

var usedNums= new Array(76);

So usedNums is undefined when you try to read from it.

You need to assign the newCard function as your load handler. So don't invoke it:

window.onload = newCard;
Sign up to request clarification or add additional context in comments.

1 Comment

@Quertin: That helped.I am pretty new to javascript. Do you mind commenting on how do we distinguish other functions form constructor in javascript?

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.