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) :):

    3 answers 3



    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; } }); }); 
    • @lampa, why aren't operations with non-integer numbers performed? 1.5 * 2 does not work ... I would be grateful if you tell me. Ps If not difficult, could you comment on each line of code. The fact is that I just started studying JS, and many things are not clear, but I really want to understand and understand how it works. Thank you in advance. - Astor
    • I did not consider a comma. Soon I will update the post with commenting. - lampa
    • @lampa, thanks, I'll be waiting. Sincerely, Alexander Ps only noticed, if we perform several operations in a row, it gives an incorrect result ... - Astor
    • Sorry, but what it is: calcScreen.val (calcScreen [0] .value); ? But in general, the solution would be to convert the input to the reverse Polish notation and perform calculations, and not use eval - Specter
    • @Spectre myself laugh: D about arrester - a calculator is simple, I think this method would be superfluous in this case. - lampa

    the 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.

    • @deivan, thanks. JavaScript is great, of course, but since the knowledge of js is a bit> 0, it will be difficult for me. Could you "kick" in the right direction (implementation steps, etc.). - Astor
    • one
      Without knowledge in JS, you should not take on jQuery (like any other JS framework). Pay attention to the fact that people do not like to spend a lot of time, and nobody is interested in poking a mouse at the buttons. Make support for keystrokes. - sorx00
    • @ sorx00, I fully agree with you, therefore, therefore, at the present time, I am actively studying JS. - Astor
    • one Here is my solution on pure JS - alex_xpert
    • one

    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:

    • @Vyacheslav Koval, did you even read the question before giving the answer ...? I think no. - Astor