简体   繁体   中英

How to access array objects inside a Function in javascript?

I'm a beginner in javascript, I have a written a javascript program for snakes and ladders using array objects, but i'm not able to understand how to call the variable snake_pos with its start and end position. Here is the code:

 var player_pos = 4; var dice_value = Math.floor(Math.random() * 6) + 1; function dice() { player_pos = player_pos + dice_value; } dice(); var last_pos = 25; function game_rule() { if (player_pos >= last_pos) { alert("won the game"); } } game_rule(); function snake() { var snake_pos = [{ start_pos: 11, end_pos: 4 }, { start_pos: 20, end_pos: 7 }, { start_pos: 24, end_pos: 3 } ] } console.log(player_pos); console.log(dice_value); console.log(snake_pos); 

You have to return your variable snake_pos to use it outside the function. After that you can call the items in the array and object , take a look at this example:

 function snake(){ // return the variable to use it outside the function return snake_pos = [ {start_pos:11, end_pos:4}, {start_pos:20, end_pos:7}, {start_pos:24, end_pos:3} ] } //store the returned value in a variable snake_pos = snake(); //log the first array item start_pos console.log(snake_pos[0].start_pos); //log the second array item start_pos console.log(snake_pos[1].start_pos); 

Simply declare snake_pos before in the same scope in which you're trying to access it:

 var player_pos = 4; var dice_value = Math.floor(Math.random() * 6) + 1; // declare it here var snake_pos = [{ start_pos: 11, end_pos: 4 }, { start_pos: 20, end_pos: 7 }, { start_pos: 24, end_pos: 3 } ]; function dice() { player_pos = player_pos + dice_value; } dice(); var last_pos = 25; function game_rule() { if (player_pos >= last_pos) { alert("won the game"); } } game_rule(); function snake() { // It will be accessible here as well } console.log(player_pos); console.log(dice_value); // It will be accessible here console.log(snake_pos); 

Check the below code and try to understand how its work.

 // declear global variables here var player_pos = 4; var last_pos = 25; var dice_value = Math.floor(Math.random()*6)+1; // function to play dice add dice value function dice(){ dice_value = Math.floor(Math.random()*6)+1; player_pos = player_pos + dice_value; } // we have snake positions in this, we check user on snake position or not we will do action accordingly function snake(){ var snake_pos = [ {start_pos:11, end_pos:4}, {start_pos:20, end_pos:7}, {start_pos:24, end_pos:3} ]; for(var i =0; i < snake_pos.length; i++) { if(snake_pos[i].start_pos == player_pos) { player_pos = snake_pos[i].end_pos; } } } // in this we define game rule, if user won we return true function game_rule(){ if (player_pos >= last_pos) { alert("won the game"); return true; } else { return false; } } // we play game until user won this game while(game_rule() != true) { console.log(player_pos); console.log(dice_value); dice(); snake(); } 

Let me know if you have any doubt.

Happy Coding !!!

 function Snake() { this.snake_pos = [{ start_pos: 11, end_pos: 4 }, { start_pos: 20, end_pos: 7 }, { start_pos: 24, end_pos: 3 } ] } var snake = new Snake(); console.log(snake.snake_pos[0].start_pos); 

Check this: https://www.phpied.com/3-ways-to-define-a-javascript-class/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM