for loop - How the get value returned and print out the list in javascript -
i create program function gets value text box , calculate pi nilakantha method. here codes html:
<p><input type="text" class="textbox" placeholder="enter number of times want calculate" id="maximum" onchange="getvalue()"></p> <p><button class="button" onclick="calculation(n)">click see caculation procedure</button></p> <p><ul id="resultlist"></ul></p>   javascript:
function getvalue(){     var n = number(document.getelementbyid("maximum").value);     if (isnan(n)){         alert("sorry should input number");}     else {return n;} } var pi = 3; function calculation(n){     (var k=1;k<n; k++){         pi = pi + (math.pow((-1),(k+1))*4)/(2*k*(2*k+1)*(2*k+2));         document.getelementbyid('resultlist').innerhtml = '<li>' + pi + '</li>'; }}   but not working @ all, value text box should maximum times program calculates. , should print list show procedure of calculating.
you got multiple problems.
first 1 didn't define n, @fuyushimoya mentioned. solved issue.
second 1 is, want print list of calculation. @ moment overwrite innerhtml in each loop.
an example appending new loops list (see last line):
function calculation(){ var n = getvalue(); (var k=1;k<n; k++){     pi = pi + (math.pow((-1),(k+1))*4)/(2*k*(2*k+1)*(2*k+2));     document.getelementbyid('resultlist').innerhtml = document.getelementbyid('resultlist').innerhtml + '<li>' + pi + '</li>';}}   but have take care once have finished 1 calculation. thinkable clear-button, emptys resultlist.
Comments
Post a Comment