3

I was trying to find the reason why my code doesnt work, and chrome comes back with this eror: Uncaught ReferenceError: calcMPG is not defined... Can someone spot my mistake ?(desperate)

<script type="text/javascript">
    function calcMPG() {
        document.calc.startingMileage.value = startMileage;
        document.calc.endingMileage.value = endMileage;
        document.calc.gallonsUsed.value = gallUsed;
        var MPG = (endMileage - startMileage) / gallUsed;
        if (isNAN(startMileage) || isNAN(endMileage) || isNAN(gallUsed)) alert('please type in numbers only!');
        else document.calc.milesPerGalon.value = MPG;
    }
</script>

<form name="calc">Starting mileage:
    <input type="text" value="0" name="startingMileage" onchange="calcMPG()">
    <br>Ending mileage:
    <input type="text" value="0" name="endingMileage" onchange="calcMPG()">
    <br>Gallons used:
    <input type="text" value="0" name="gallonsUsed" onchange="calcMPG()">
    <br>Miles per galon:
    <input type="text" value="0" name="milesPerGalon">
</form>
11
  • 1
    startMileage is undefined. Full code please. Commented Sep 1, 2013 at 17:59
  • When I run this, I get the error "Uncaught ReferenceError: startMileage is not defined", not calcMPG as you specified. Commented Sep 1, 2013 at 17:59
  • 1
    What's startMileage, endMileage and gallUsed ? Commented Sep 1, 2013 at 18:00
  • I'd get some jQuery into the mix and use listeners instead of onChange attrs. And of course, those listeners wouldn't trigger until the DOM is ready. Commented Sep 1, 2013 at 18:01
  • It's isNaN, not isNAN. Commented Sep 1, 2013 at 18:02

4 Answers 4

6

Your declarations are wrong please correct them.

var startMileage = document.calc.startingMileage.value ;
var endMileage =  document.calc.endingMileage.value;
var gallUsed =  document.calc.gallonsUsed.value;
Sign up to request clarification or add additional context in comments.

Comments

1

I don't know why you do that, but try this:

<script type="text/javascript">
  function calcMPG(){
    var startMileage  = document.calc.startingMileage.value,
        endMileage    = document.calc.endingMileage.value,
        gallUsed      = document.calc.gallonsUsed.value,
        MPG           = (endMileage - startMileage) / gallUsed;

      if(isNaN(startMileage) || isNaN(endMileage) || isNaN(gallUsed)){
        alert('please type in numbers only!');
      } else {
        document.calc.milesPerGalon.value = MPG;
      }
    }
</script>
<form name="calc">
    Starting mileage:<input type="text" value="0" name="startingMileage" onchange="calcMPG()"><br>
    Ending mileage:<input type="text" value="0" name="endingMileage" onchange="calcMPG()"><br>
    Gallons used:<input type="text" value="0" name="gallonsUsed" onchange="calcMPG()"><br>
    Miles per galon:<input type="text" value="0" name="milesPerGalon">
</form>

Comments

1
startMileage

and your other right side references don't mean anything, they are undefined variables.

Give your inputs an id and fetch the values from them like this:

var startingMileage = document.getElementById('startingMileage').value;

2 Comments

should be document.getElementById('startingMileage').value;
@PatrickEvans Yes, correct, I made too much haste there. Updated.
0

Check here Fiddle

 function calcMPG(){
        var startMileage = document.calc.startingMileage.value;
        var endMileage = document.calc.endingMileage.value;
        var gallUsed = document.calc.gallonsUsed.value;

        var MPG = (endMileage - startMileage) / gallUsed; 

        if(isNaN(startMileage) || isNaN(endMileage) || isNaN(gallUsed))
            alert('please type in numbers only!');
        else 
            document.calc.milesPerGalon.value = MPG;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.