error in javascript:TypeError:Cannot read property '2' of undefined

Asked 1 years ago, Updated 1 years ago, 70 views

I looked into what was wrong...
I don't know because I have included arguments.

TypeError: Cannot read property '2' of undefined
constboard_preference={0:"+", 1:"○", 2:"●", 3:"■", 4:"+"};
letboard= 
[[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,4,3,0,0,0],
[0,0,4,1,2,3,0,0],
[0,0,3,2,1,4,0,0],
[0,0,0,3,4,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0]];

const user_turn=(user_selection_vertical, user_selection_horizontal)=>{
    var user_x = user_selection_vertical;
    var user_y = user_selection_horizontal;
    if(board[user_x][user_y]==3){
        board [user_selection_vertical] [user_selection_horizontal] = 1;
    }
    let user_turn_flag = true;
    let user_turn_count = 0;
    while(user_turn_flag){
        user_turn_count++;
        if(!user_x+user_turn_count==0){
            if (board [ user_x-user_turn_count ][user_y]==2&board [user_x-(user_turn_count+1)][user_y]==1){
                changehost(user_x-user_turn_count, user_y, 1);
            }
        }
        if(!user_x+user_turn_count==7){
            if (board [ user_x + user_turn_count ][user_y]==2&board [user_x+(user_turn_count+1)][user_y]==1){
                changehost(user_x-user_turn_count, user_y, 1);
            }
        }
        if(!user_y+user_turn_count==0){
            if(board[user_x][user_y-user_turn_count] == 2 & board [user_x] [user_y-(user_turn_count+1)] == 1) {
                changehost(user_x-user_turn_count, user_y, 1);
            }   
        }
        if(!user_y+user_turn_count==7){
            if(board[user_x][user_y+user_turn_count] == 2 & board [user_x] [user_y+(user_turn_count+1)] == 1) {
                changehost(user_x-user_turn_count, user_y, 1);
            }
        }
        if(user_turn_count==7) {user_turn_flag=false;}
    }
}

const changehost=(host_selection_vertical, host_selection_horizontal, Human)=>{
    let host_x = host_selection_vertical;
    let host_y = host_selection_horizontal;
    board [host_x] [host_y] = Human;
}
user_turn(4,2);
console.log(board_display());

javascript node.js

2022-09-30 11:39

1 Answers

While it is unclear whether the code is sufficient for the intended code, the following points can be pointed out as incorrect:

1) When to increase the loop counter user_turn_count

user_turn_count is assumed to be 0 or higher 7 or lower, but with this code, +1 immediately after entering the will loop (and if(user_turn_count==7){user_turn_count=7) {usernode; false }

I think the timing of increase should be after if(user_turn_count==7)....

Also, in this case, is more appropriate than while.

2) operator priority

For example, the first if statement in the while statement

 if(!user_x+user_turn_count==0){

I believe is meant to be "if the result of adding user_x and user_turn_count is not 0", but in fact, the result of adding user_x and user_turn_count is 0.

This is because ! has a higher priority than ==.
Description is provided at the following link:

3)ifAssessment Criteria

whileSecond if statement

 if(!user_x+user_turn_count==7){
    if (board [ user_x + user_turn_count ][user_y]==2&board [user_x+(user_turn_count+1)][user_y]==1){
        changehost(user_x-user_turn_count, user_y, 1);
    }
}

If you look at , the index specified in the board array is user_x+user_turn_count (e.g., ), which also appears in the if statement, it is considered to be a conditional determination to ensure that the index of the board array is within range.

Therefore, the code for the expected behavior is actually

 if(user_x+user_turn_count<=7)

Is that so?
(*However, the index also specifies user_x+(user_turn_count+1), so I don't think the above specification is correct.)

Considering the above, the code is as follows (as I mentioned at the beginning, I'm not sure if this is enough):

constboard_preference={0:"+", 1:"○", 2:"●", 3:"■", 4:"+"};
letboard= 
[[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,4,3,0,0,0],
[0,0,4,1,2,3,0,0],
[0,0,3,2,1,4,0,0],
[0,0,0,3,4,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0]];

const user_turn=(user_selection_vertical, user_selection_horizontal)=>{
var user_x = user_selection_vertical;
var user_y = user_selection_horizontal;
if(board[user_x][user_y]==3){
    board [user_selection_vertical] [user_selection_horizontal] = 1;
}
let user_turn_flag = true;
let user_turn_count = 0;
while(user_turn_flag){
    if(user_x-user_turn_count>0){
        if (board [ user_x-user_turn_count ][user_y]==2&board [user_x-(user_turn_count+1)][user_y]==1){
            changehost(user_x-user_turn_count, user_y, 1);
        }
    }
    if(user_x+user_turn_count<7){
        if (board [ user_x + user_turn_count ][user_y]==2&board [user_x+(user_turn_count+1)][user_y]==1){
            changehost(user_x-user_turn_count, user_y, 1);
        }
    }
    if(user_y-user_turn_count>0){
        if(board[user_x][user_y-user_turn_count] == 2 & board [user_x] [user_y-(user_turn_count+1)] == 1) {
            changehost(user_x-user_turn_count, user_y, 1);
        }   
    }
    if(user_y+user_turn_count<7){
        if(board[user_x][user_y+user_turn_count] == 2 & board [user_x] [user_y+(user_turn_count+1)] == 1) {
            changehost(user_x-user_turn_count, user_y, 1);
        }
    }
    if(user_turn_count==7) {user_turn_flag=false;}
    user_turn_count++;
}
}

const changehost=(host_selection_vertical, host_selection_horizontal, Human)=>{
let host_x = host_selection_vertical;
let host_y = host_selection_horizontal;
board [host_x] [host_y] = Human;
}

user_turn(4,2);
console.table(board);

Code difference


2022-09-30 11:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.