Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaScript button click to change variable
    primarykey
    data
    text
    <p>Hey people I'm doing a complicated statistics project in which I have several numbers displayed and want to change how many decimals they're calculated to. I want to make it so that the viewer of the page can change this number by clicking an increase or decrease button. But right now the only way I know how to change it is through the code itself, which is the "dec" variable at the beginning. Here I have it set to 2 places, but how could I make it so that it changes value with the buttons? Thanks!</p> <pre><code>&lt;input type="button" onclick="increase()" value="Increase"/&gt; &lt;input type="button" onclick="decrease()" value="Decrease"/&gt; &lt;br/&gt;&lt;table border="1"&gt;&lt;tr&gt; &lt;script type="text/javascript"&gt; dec=2; for(n=1;n&lt;=9;n++) { document.write("&lt;td&gt;"); for(t=0;t&lt;=45;t++) { if(t&lt;n) result=0; else result=fixed(((combin(t,n)*fact(n)*fact(45-n)/fact(45))*100),dec); document.write(result); zero(result,dec); document.write("%&lt;br/&gt;"); } document.write("&lt;/td&gt;"); } function increase() { ??? } function decrease() { ??? } function fact(num) { result=1; for(cnt=num;cnt&gt;0;cnt--) result*=cnt; return result; } function combin(n,r) { result=fact(n)/(fact(r)*fact(n-r)); return result; } function fixed(num,d) { result=Math.round(num*Math.pow(10,d))/Math.pow(10,d); return result; } function zero(num,dec) { if(dec!=0&amp;&amp;num-Math.floor(num)==0) document.write("."); for(cnt=dec;cnt&gt;0;cnt--) { test=num*Math.pow(10,cnt) if(test%10==0) document.write("0"); } } &lt;/script&gt;&lt;/tr&gt;&lt;/table&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload