javascript - Array converted into a string is used to be converted into a number but does not work -
i building calculator html, css , and pure javascript. tried little information, cannot understand happening now. since person type many digits (i.e 456) create number , click on operator, decided create array(arrayinput) hold numbers 4,5,6 , character "." instance 4.56 in array appears 4,.,5,6 removed characters , whitespaces. since string "4.56" used number() function convert number. used parseint, parsefloat, displays first time , when operator selected, not calculate total, remains total zero. appreciate support.
<!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title> - jsfiddle demo</title> <style type='text/css'> .row{ height: 2em; width: 25%; border: 0.05em solid black; float:left; font-size: 2em; font-style: sans-serif; text-align:center; padding:0; } #screen{ height: 4em; width: 100%; border: 0.03em solid black; float:left; font-size: 3em; font-style: sans-serif; text-align:right; margin:0; padding:0; } </style> <script type='text/javascript'> var total=0; //which replace number1 in parameters var numberarray=[]; var operator="plus"; //by default give addition var number=0; //get digits buttons create number want use var performoperator=function (tot,num,ope){ if(ope==="plus") { tot+=num; num=0; document.getelementbyid("screen").innerhtml = tot; //display number of total } else if(ope==="-") { tot-=num; num=0; document.getelementbyid("screen").innerhtml = tot; //display number of total } else if(ope==="multiply") { tot*=num; num=0; document.getelementbyid("screen").innerhtml = tot; //display number of total } else if(ope==="/") { tot/=num; num=0; document.getelementbyid("screen").innerhtml = tot; //display number of total } else if(ope==="equal") { document.getelementbyid("screen").innerhtml = tot; //display number of last total num=0; } else if(ope==="clear") { tot=0; num=0; document.getelementbyid("screen").innerhtml = tot; //display number of total } } function getdigit(elemid){ var digitentry=document.getelementbyid(elemid).value; //digit value goes "val" numberarray.push(digitentry); //each val should go push array var numberwithspace=numberarray.join(' '); //convert array string block spaces var numbernospace=numberwithspace.replace(/\s/g, ''); //string number no space number=number(numbernospace);// should converter number here not like.. document.getelementbyid("screen").innerhtml = number; //probably displays array of number string } function highlightsign(elemid){ var sign=document.getelementbyid(elemid).value; //digit value goes "sign"" operator=sign; performoperator(total,number,operator); //call performoperator function , creates new total , new number 0 numberarray=[]; //once number created real number array used built clears } </script> </head> <body> <body> <p id="screen"></p> <button id="nc" class="row" value=""></button> <button id="pmsign" class="row" value=""></button> <button id="npercentage" class="row" value=""></button> <button id="divide" class="row" value="/" onclick="highlightsign(this.id)">/</button> <button id="n7" class="row" value=7 onclick="getdigit(this.id)">7</button> <button id="n8" class="row" value=8 onclick="getdigit(this.id)">8</button> <button id="n9" class="row" value=9 onclick="getdigit(this.id)">9</button> <button id="opx" class="row" value="multiply" onclick="highlightsign(this.id)">x</button> <button id="n4" class="row" value=4 onclick="getdigit(this.id)">4</button> <button id="n5" class="row" value=5 onclick="getdigit(this.id)">5</button> <button id="n6" class="row" value=6 onclick="getdigit(this.id)">6</button> <button id="minus" class="row" value="-" onclick="highlightsign(this.id)">-</button> <button id="n1" class="row" value=1 onclick="getdigit(this.id)">1</button> <button id="n2" class="row" value=2 onclick="getdigit(this.id)">2</button> <button id="n3" class="row" value=3 onclick="getdigit(this.id)">3</button> <button id="plus" class="row" value="plus" onclick="highlightsign(this.id)">+</button> <button id="n0" class="row" value=0 onclick="getdigit(this.id)">0</button> <button id="clear" class="row" value="clear" onclick="highlightsign(this.id)">clear</button> <button id="decpoint" class="row" value="." onclick="getdigit(this.id)">.</button> <button id="equal" class="row" value="equal" onclick="highlightsign(this.id)">=</button> </body> </body> </html>
i made total work here. need change logic of calculator. there's lot change there
http://jsfiddle.net/g6gj1kr6/9/
don't pass total parameter make global, can rewrite it.
var tot=0;
anyway, there lot of work calculator
Comments
Post a Comment