Jump to content

Problem With Array


rogerg

Recommended Posts

Hello, I would like to do some calculations in a predefined array. With the script below I can retrieve a number of the matrix.However, I want to reuse this number and do some calculations with it, for example calculate the factorial. But I don't now how I have to save the selected number and then to reuse it for further calculation. can you help me? Thx,Roger <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"> <head><title>MATRIX</title> </head><body><p>MATRIX</p><form action=''><table> <tr><td>Size:</td><td><input type='text' id='size' value='' /></td></tr> <tr><td>Line:</td><td><input type='text' id='line' value=''/></td></tr> <tr><td>Col:</td><td><input type='text' id='col' value=''/></td></tr> <tr><td>Selected:</td><td><input type='text' id='selected' value=''/></td></tr></table></form> <input value="show" type='button' onclick="see(document.getElementById('size').value/1, document.getElementById('line').value/1,document.getElementById('col').value/1)"/></div> <script>function matrix(a){ var myMatrix=new Array(); var value=1; for(var i=0; i<a; ++i){ myMatrix=new Array(); for(var j=0; j<a; j++){ myMatrix[j]= value; ++value; } }return myMatrix;}function see(x,y,z){ if(y>x-1 || y<0){ document.getElementById('selected').value="Error"; document.getElementById('line').className='error'; document.getElementById('line').value="Number between 0 and "+(x-1)+" please!"; } else if(z>x-1 || z<0){ document.getElementById('selected').value="Error"; document.getElementById('col').className='error'; document.getElementById('col').value="Number between 0 and "+(x-1)+" please!"; } else{ myMatrix=matrix(x); document.getElementById('line').className='correct'; document.getElementById('selected').value=myMatrix[y][z]; }}</script></body></html>

Link to comment
Share on other sites

If I understand correctly, all you need to do is create the variable in the global space (ie, not in a function). If this: myMatrix=matrix(x); is the variable you want to save, I would recommend renaming it to something besides myMatrix to avoid confusion with the variable in your matrix function. Then declare it globally:

var currMatrix;function matrix(...) {  ...}function see(...) {  if (...) {    ...  } else {    currMatrix = matrix(x);    ...  }}

Link to comment
Share on other sites

thx for your answer what I'm trying to reuse is the result is the selected value so, if a user types insize 3line 1col 1 the value I want to reuse is 5, so what I have in function see ... document.getElementById('selected').value=myMatrix[y][z] How can I declare this value to reuse it at once for example to calculate and show it's factorial? I try doing so by adding a button factorial in the form with a onclick"function()", but when trying to declare the function I keep getting is not defined errors... do I have to declare a new variable already at the end of the see function or at the beginning of the factorial function?I'm quite confused as I'm not too familiar with javascript yet

Link to comment
Share on other sites

Ok, then declare your variable globally like I showed you before. This time I'll call it matrixVal. Then replace: document.getElementById('selected').value=myMatrix[y][z]; with: matrixVal = myMatrix[y][z];document.getElementById('selected').value = matrixVal; Now you should be able to use matrixVal in other functions.

Link to comment
Share on other sites

I could retrieve the matrixVal value, but only inside of the see function when I tested with an alert outside of the function, it didn't work... I can use the result within the function and do simple calculations like multiplications with it, but I haven't figured out how to do the factorial as I can't retrieve the matrixVal outside of my see function. Is there a way in JS to add a function within a function, i.e. could I add a factorial function like I tried within my see function? within the function I could do something like... L=(matrixVal*(matrixVal-1)*(matrixVal-2)*(matrixVal-3));document.form.L.value=L; if I knew the value of matrixVal in the specific case but of course it would be nicer to have a function which works all the time. But with a loop I would need a new function and cannot just do it within the see function

Link to comment
Share on other sites

I pulled the code from your first post and made the changes I suggested. It seems to do exactly what you want:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>MATRIX</title></head><body><p>MATRIX</p><form action=''><table><tr><td>Size:</td><td><input type='text' id='size' value='' /></td></tr><tr><td>Line:</td><td><input type='text' id='line' value=''/></td></tr><tr><td>Col:</td><td><input type='text' id='col' value=''/></td></tr><tr><td>Selected:</td><td><input type='text' id='selected' value=''/></td></tr></table></form><input value="show" type='button' onclick="see(document.getElementById('size').value/1,document.getElementById('line').value/1,document.getElementById('col').value/1); alert(factorial(matrixVal));"/></div><script>var matrixVal; //Declare matrixVal globallyfunction matrix(a){    var myMatrix=new Array();    var value=1;    for(var i=0; i<a; ++i){        myMatrix[i]=new Array();        for(var j=0; j<a; j++){            myMatrix[i][j]= value;            ++value;        }    }    return myMatrix;}function see(x,y,z){    if(y>x-1 || y<0){        document.getElementById('selected').value="Error";        document.getElementById('line').className='error';        document.getElementById('line').value="Number between 0 and "+(x-1)+" please!";    }    else if(z>x-1 || z<0){        document.getElementById('selected').value="Error";        document.getElementById('col').className='error';        document.getElementById('col').value="Number between 0 and "+(x-1)+" please!";    }    else{        myMatrix=matrix(x);        document.getElementById('line').className='correct';        matrixVal = myMatrix[y][z]; //Store the selected matrix value in matrixVal for later use        document.getElementById('selected').value = matrixVal;    }}function factorial(n) {    var f = n;    for (var i=n-1; i>0; i--) {        f = f*i;    }    return f;}</script></body></html>

I also provided a factorial function to show you that it does work. I modified the onclick of the button to alert the factorial of the selected value.

Link to comment
Share on other sites

...I also provided a factorial function to show you that it does work. I modified the onclick of the button to alert the factorial of the selected value.
thank you very much not sure whether I tried it with alert - I tried to show the value directly in the form...I'm now managed to do that with the addition of a button in the form and the correct onclick command
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...