javascript - Why value of equation has undefined at the end? -


i write simple calculator in js & jquery , i've come across strange behaviour. in code below, see variable equation holds every number pressed on calculator. use eval() well, evaluate.

issue is, value of equation variable you'd expect undefined @ end, , cannot figure out why.

whole project here: http://codepen.io/ketus/full/xmyrov/

thanks in advance!

ps.: know general opinion eval(). used testing can moving. implement own solution after solving problem undefined.

$(document).ready(function() {          var equation = '';    var input = $('h2[data-value]');      var info = $('#info');    //get keys    var keys = $('.key').filter(function() {      return $(this).data('key');    }).get();          //get operators    var operators = $('.operator').filter(function() {      return $(this).data('operator');    }).get();            //clearing input    $('.clear').click(function(){      equation = '';      input.html('');      info.html('');    });          //user press keys, check if correct after each click     $('.key').click(function(){      equation += $(this).data('key');        //evaluateequation();      input.html(equation);        });           //showing result    $('a[data-calc]').click(function(){      input.html(eval(equation.tostring()));    });         // rules equation correct    //function evaluateequation()  {        //}        function calculate(){          if(equation.length > 0){            return eval(equation);      } else {         info.html('enter something!');        return '';      }     }      });

the main problem user press event handler. should not append data('key') if = pressed. example can exclude button this:

  //user press keys, check if correct after each click    $('.key:not([data-calc])').click(function(){     equation += $(this).data('key');       input.html(equation);       });    

demo: http://codepen.io/anon/pen/qokwoe?editors=101


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -