Good day!
Please tell me how to make a calculator using jQuery. I was a little magician, and that's what I did (did not work) :):
Good day!
Please tell me how to make a calculator using jQuery. I was a little magician, and that's what I did (did not work) :):
\about
about/
How do you add numbers in your mind? First, remember the first number, then add the first one to the second number. Those. The script for each operation should keep the penultimate value. Further usual checks go.
$(function() { var lastNum = false; var lastOperator = ''; calcScreen = $('#screen input'); calcScreen[0].value = '0'; // $('#number li a, #operators li a, #result').click(function() { /** * clear */ if(this.text.toLowerCase() == 'c') { lastNum = 0; calcScreen.val(0); return; } /** * end */ if(this.text.toLowerCase().trim() == '=') { if(!lastNum) { calcScreen.val(calcScreen[0].value); return; } calcScreen.val(eval(String(lastNum) + String((/\w/.exec(calcScreen[0].value))?String(lastOperator) + calcScreen[0].value:''))); return; } /** * operators */ if(/^\W/.exec(this.text)) { if(lastNum) { calcScreen.val(eval(String(lastNum) + String((/\w/.exec(calcScreen[0].value))?String(lastOperator) + calcScreen[0].value:''))); } lastNum = calcScreen.val(); calcScreen.val(this.text); lastOperator = this.text; return; } /** * echo */ if(calcScreen[0].value == '0' || (/^\W/.exec(calcScreen[0].value))) { calcScreen[0].value = this.text; } else { calcScreen[0].value += this.text; } }); });
eval
- Specterthe layout is beautiful, but the functional that should perform calculator functions is not as such.
Start by solving this problem in pure javascript, jkveri here and not really needed.
I can advise you to look in the direction of RightJS . There it is easier to understand, there are examples of games, for example on ibm developerworks there are 2 articles on how to write a sapper on RightJS:
Source: https://ru.stackoverflow.com/questions/161932/
All Articles