Friday, 13 September 2013

How to evaluate a reciprocal function

How to evaluate a reciprocal function

I am building a basic jQuery calculator. Everything was going good, until
I tried doing the reciprocal function. I have it bound to a click event.
The code looks like this
// inverse is $(".row li.inverse")
inverse.on("click", function () {
// textBox is $("input#result")
var val = textBox.val(),
text = parseFloat(val),
recip = eval(1/text);
textBox.val(recip);
});
So on the click of the button with the class inverse, it should get the
value of what is in the input, and turn it into a number. It should then
eval the number as 1 divided by the number, and set the textbox value
equal to the answer. But when I click the button, the value stays the
same. However, when I put the code into firebug without the click handler,
it works just fine. Where am I going wrong?
Fiddle

No comments:

Post a Comment