0

I'm making a javascript/html calculator for school. Everything is working fine except for the division. Here is the code:

if(x==="/"){
                memory1 = parseFloat(memory1);
                memory2 = parseFloat(memory2);
                result = parseFloat(result);
                if(result > 0){
                    result = result / memory1;
                    document.TextBox.Answer.value = result;
                    memory1 = "";
                    last = "/";
                }

The result text box shows 'NaN' even though I'm converting to a Float. We're using Chrome for Mac & Windows.

Wooble
  • 87,717
  • 12
  • 108
  • 131
Roemer
  • 1,336
  • 1
  • 15
  • 39
  • 8
    What's the initial values of `memory1`, `memory2` and `result`? I'll bet one of them couldn't be converted to a float :) – CodingIntrigue Oct 01 '13 at 12:23
  • 5
    Is there a school currently teaching a course "How to build a calculator using StackOverflow"? This question has popped up a lot of times the past few days. – Stephan Muller Oct 01 '13 at 12:25
  • Try !isNaN on the values you parse (and I presume a 0 could give a NaN too) See http://stackoverflow.com/questions/15478609/javascript-parsefloat-and-nulls – doctorlove Oct 01 '13 at 12:27
  • You don't have to use *parseFloat*: `result = memory1 / memory2` will do implicit conversion to number. Best though to test that *memory1* and *memory2* are valid numbers before doing anything and test the result before returning it. – RobG Oct 01 '13 at 12:59

0 Answers0