1

i want to pass an array to a function and work on it,but i am afraid ,error occurs saying board_pt is undefined. What is the problem? This is the code :

function set_boardPoint( board_pt,turn)
{   
    var no = board_pt.number-1;
    board[no].row = board_pt.row;
    board[no].col = board_pt.col;
    board[no].x = board_pt.x;
    board[no].y = board_pt.y;
    board[no].value = turn;
    board[no].number = board_pt.number;
}

board is a global array already defined

5
  • 5
    can you show how you're calling set_boardPoint? Commented Feb 4, 2011 at 8:42
  • 1
    I would suggest you to concentrate on the error message. If board_pt is undefined then it probably means what it says. Commented Feb 4, 2011 at 8:44
  • var previous_boardPoint=board[i]; Commented Feb 4, 2011 at 8:46
  • set_boardPoint(previous_boardPoint,0);This is the way i am calling it Commented Feb 4, 2011 at 8:50
  • And how is previous_boardPoint defined? Commented Feb 4, 2011 at 9:02

3 Answers 3

1

The problem is that board_pt have only 1 item, and js in these case know board_pt as object:

function set_boardPoint( board_pt,turn)
{   
    var no = board_pt.number-1;
  if( board[no] != undefined ) 
  {
    board[no].row = board_pt.row;
    board[no].col = board_pt.col;
    board[no].x = board_pt.x;
    board[no].y = board_pt.y;
    board[no].value = turn;
    board[no].number = board_pt.number;
}else
{
 board.row = board_pt.row;
    board.col = board_pt.col;
    board.x = board_pt.x;
    board.y = board_pt.y;
    board.value = turn;
    board.number = board_pt.number;
}
}
Sign up to request clarification or add additional context in comments.

Comments

0

If board_pt is a real array, then it's unlikely that it has a property named number. Do you mean length?

2 Comments

the array board[i] has board[i].row,...board[i].number and i passed it like this : previous_boardPoint=board[i]; where var previous_boardPoint=new Array();
ok i got that as u told ,i am passing object to the function but it still says board_pt is undefined.actually board[] is an array of objects.
0

From your comments, you have to define the previous_boardPoint as an array. To declare a array in javascript, you need to have - var previous_boardPoint = []; in your code. Also have each elements defined (if you have any) like previous_boardPoint[0] = val1; previous_boarPoint[1] = val2; etc.

If these things are not in your code, then in all likely possibilities, you will get board_pt as undefined in your function set_boardPoint.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.