drayarms Posted June 15, 2012 Share Posted June 15, 2012 Hi everyone, I can't seem to figure out if the following problem is actually a variable scope problem or not. To undertand the problem, it's best if you just go right ahead and take a look at the code. It's an over simplified version of the actual code. Pay close attention to the explanations within the code especially within the modify_b() function which is where the heart of the problem is. The problem is well described within the explanation lines. $(document).ready(function() {//Check var a = [1,2,3,4,5]; $("#div").click(function(){//On clicking some div, the following should happen var start = "yes" if(start != "yes"){//Alert error alert("error"); }else{//Proceed var b = [6,7,8,9,10]; function modify_b(){ var new_b = b;//create a new array out of b array whihc we will modify below var c = b.shift();//Obtain the element at begining of b array alert(c);//When the run_modify_b function(see below) is run, 6 is alerted, an indication that the array b is actually accessible from within this function var l = b.length; alert(l);//But when we try to obtain the length or array b, we get 0. Makes me wonder if this array is really accessible from within this function. Mind you, this same line when taken outside the fucntion, gives us the desired 5. Same thing happes with new_b for(var i=0;i<b.length;i++){alert(b[i]);}//Obviously, this line (which is what I originally wanted to to do with my function), does nothing as b.length is 0. Again, when taken outside the function, this line alerts the desired 6,7,8,9,10 if($.isArray(b)){alert("aye!");}else{alert("nay!");}//Alerts aye!, an indication that b (also new_b) is actually recognized as an array within this function. So why in the world can't normal array methods be performed on this array?? Beats me. //Now I tested all the above functions on array a and got the desired results as follows var l = a.length; alert(l);//alerts 5 for(var i=0;i<a.length;i++){alert(a[i]);}// alerts 1,2,3,4,5 }//End of modify b function function run_modify_b(){ modify_b(); }//End of run modify b function //Run the run modify b function. This may appear redundant but in the original code, there is much more than just this one line run_modify_b(); }//End of else proceed. });//End of div click function });//End document ready method So, Im really confused. Is this a variable scope problem where the modify_b() function cant access the b variable (i really don't see any reason why), in which case, why would it even be modifiable within that function or why would it be recognized as an array? please help. Quote Link to comment https://forums.phpfreaks.com/topic/264223-could-this-be-a-variable-scope-problem/ Share on other sites More sharing options...
kicken Posted June 15, 2012 Share Posted June 15, 2012 Your posted example code works just fine. If your going to edit down a problem into sample code, the sample code needs to at least re-produce the issue you are having. Without the real code, or a representative example, it's hard to provide any help. It sounds like a case where you might need to add a closure to lock a value in scope, but I could be wrong. If you can't or don't want to provide the actual code, try to re-create the issue on JSFiddle and then give us the link to or a copy that code. Quote Link to comment https://forums.phpfreaks.com/topic/264223-could-this-be-a-variable-scope-problem/#findComment-1354068 Share on other sites More sharing options...
drayarms Posted June 15, 2012 Author Share Posted June 15, 2012 @kicken, ok i tried to keep the code as original as possible, cutting out parts that are irrelevant to this problem while trying to still get the program working as intended. Here's the link to the fiddle http://jsfiddle.net/w8gDg/ You could ignore everything before line 88 on the js section because these are just functions which have been defined and are used by various throughout the program. To put you in perspective, it is a simple card game. The array a which i described in the problem could be one of the following in the actual code, var deck (line91), var clubs(line 103), var aces(line107), var twos(line 109), or var threes(line11). The array b corresponds to b1cards(line191). modify_b() function corresponds to computer_play() fucntion (line240). This function is called within the whose_turn() function on line 352 PS, The output on the fiddle may flicker when you first load it and you may need to hit "run a couple of times". And in the input area, limit the number to be entered in the input box to 3 or 4 for best viewing results. Player 1 container would be the first row of images, the row right beneath it (1 column) is board1, and beneath that (also 1 column) is board two and finally the bottom row, player 2 container. Quote Link to comment https://forums.phpfreaks.com/topic/264223-could-this-be-a-variable-scope-problem/#findComment-1354193 Share on other sites More sharing options...
Mahngiel Posted June 15, 2012 Share Posted June 15, 2012 man, u sure have some serious love for the return key... Quote Link to comment https://forums.phpfreaks.com/topic/264223-could-this-be-a-variable-scope-problem/#findComment-1354211 Share on other sites More sharing options...
kicken Posted June 15, 2012 Share Posted June 15, 2012 Not sure I really follow what the game is supposed to be doing. In particular the way you seem to be dealing your cards is rather confusing. If you just want to give each player a random set of num_cards each then all you have to do is something as simple as: var new_deck = deck.slice(0); //Make a replica of shuffled deck array fisher_yates(new_deck);//Shuffle the deck var p1cards = new_deck.splice(0, num_cards); //Extract cards from the deck for player 1 var p2cards = new_deck.splice(0, num_cards); //Extract cards from the deck for player 2 var b1cards = new_deck.slice(0); //The remainder of the deck That's basically like shuffling a deck then taking the top num_cards for player one, then again for player 2. The splice method removes the elements from the original array so there would not be any duplicates between the three end result arrays. What is the game and how does it work/what are the rules? Might help me to understand the code you have written. Quote Link to comment https://forums.phpfreaks.com/topic/264223-could-this-be-a-variable-scope-problem/#findComment-1354212 Share on other sites More sharing options...
drayarms Posted June 15, 2012 Author Share Posted June 15, 2012 @kicken, well, I'm going to take your advice and use the splice() method to simplify things. Not sure if it would solve this particular problem though. The game is very similar to uno. Same basic principles. Main difference here is that we use the traditional pack of 54 cards instead of the colored uno cards. The rules wouldn't really matter at this stage as all I'm trying to accomplish at this point is to deal the cards, indicate who's turn it is(computer vs player), and remove a random card from the computer's deck, when it is the computer's turn and place that in the play area(board1), and do the same for the player's deck when it's player's turn and player click in his play area(player2container). Then I will worry about the intricaces of deciding exactly what card the computer should play and and the animations later. Quote Link to comment https://forums.phpfreaks.com/topic/264223-could-this-be-a-variable-scope-problem/#findComment-1354214 Share on other sites More sharing options...
drayarms Posted June 17, 2012 Author Share Posted June 17, 2012 @kicken, well, I solved the problem by making a replica of the array (p1cards) right after it was originally declared. Then inside the computer_play() fucntion, I used this replica, not the p1cards array itself, and got expected results. Quote Link to comment https://forums.phpfreaks.com/topic/264223-could-this-be-a-variable-scope-problem/#findComment-1354659 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.