how to make 2d array in function global | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 5

how to make 2d array in function global

i have make a web code, which i want to create a global 2d array, but that array seems like not global, may any one please give me some hints on how to make that happen, thx for the code, please see my shared codes, it is the only public code that i have

3rd Sep 2017, 5:26 AM
EnRico Lam
EnRico Lam - avatar
7 Respuestas
+ 5
Check my answer on this thread: https://www.sololearn.com/discuss/671863/?ref=app It could help you to understand how closure (and scope of variables) works ^^
3rd Sep 2017, 5:55 AM
visph
visph - avatar
+ 4
That's because you declared the array within a function.
3rd Sep 2017, 5:38 AM
Dapper Mink
Dapper Mink - avatar
+ 4
but i must do that, because i need to call it for each tr, is there a method that i could make it global?
3rd Sep 2017, 5:39 AM
EnRico Lam
EnRico Lam - avatar
+ 4
No you mustn't do that, you can declare it outside of the function then append values in it everytime the button is clicked
3rd Sep 2017, 5:40 AM
Dapper Mink
Dapper Mink - avatar
+ 4
Declare it outside the function and set it inside the function. You also have several HTML errors. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="container"> <table> <tr id="row1" onclick="find(this)"> <td>foo1</td> <td>bar1</td> <td class="hidden"></td> <!-- hidden is a class --> </tr> <tr id="row2" onclick="find(this)"> <td>foo2</td> <td>bar2</td> </tr> <tr id="row3" onclick="find(this)"> <td>foo2</td> <td>bar2</td> </tr> </table> <input type="textbox" id="row" placeholder="row"> <!-- input tags are self closing --> <input type="textbox" id="col" placeholder="column"> <!-- input tags are self closing --> <button onClick="btn()">click me</button> <!-- there isn't a button function yet --> </div> <!-- You forgot your closing div tag --> <script> var td_sum; // declared outside of function to make global function find(element){ var tr_sum = element.parentNode.childElementCount; //count tr total var ntr = element.id.substr(3); //get sequence of selected row alert(tr_sum); td_sum = new Array(tr_sum); // set inside function td_sum[ntr] = element.childElementCount; //count td in selected row alert(td_sum[ntr]) } $('#row2').click() </script>
3rd Sep 2017, 5:51 AM
ChaoticDawg
ChaoticDawg - avatar
+ 4
well, i have changed the code, i now have a array content[row][col], in this case, what should i declare outside the function? content[] or content[][]?
3rd Sep 2017, 6:02 AM
EnRico Lam
EnRico Lam - avatar
+ 4
You need to declare the name outside, and you can assign value anywhere: var myVar; myFunction() { myVar = new Array(); myVar[0] = new Array(); /* ... */ } You can even write it shorter: myVar = []; myVar[0] = []; And also, you can test if the variable is already defined (to not overwrite it): if (myVar===undefined) { myVar = ... }
3rd Sep 2017, 6:09 AM
visph
visph - avatar