0

I have been attempting to figure out how to create a distance calculator. I think the formula is correct, but my issue is that my variables will not run through the formula or even display in my output.

<!doctype html>
<!-- distance.html                     Mason Boroff -->
<!-- =================================================== -->
<html>
<head>
    <title> Distance Calculator </title>

    <script type="text/javascript">
    function ShowDistance()
        {
        var x1, x2, y1, y2;
            x1=parsefloat(document.getElementById('xOne').value);
            x2=parsefloat(document.getElementById('xTwo').value);
            y1=parsefloat(document.getElementById('yOne').value);
            y2=parsefloat(document.getElementById('yTwo').value);

        var distance = Math.sqrt( Math.pow((x1-x2), 2) + Math.pow((y1-
y2), 2) );
        return distance;

            document.getElementById('outPut').innerHTML=
                 'The distance bewtween (' + x1 + ',' + y1 + ') and (' 
+ x2 + ',' + y2 + ') is '+ distance +;
        }
    </script>
</head>

<body>
    <h2>Distance Calculator</h2>
    <p>
        Coordinate 1: (<input type="text" id="xOne" size=12 value=''> , 
<input type="text" id="yOne" size=12 value=''>)
        <br>
        Coordinate 2: (<input type="text" id="xTwo" size=12 value=''> , 
<input type="text" id="yTwo" size=12 value=''>)

        <br>
        <br>
        <input type="button" onclick="ShowDistance()" value="Calculate 
Distance";>
    </p>
    <hr>
    <div id="outPut"> </div>
</body>

4
  • 2
    You return before printing out your output. So the line document.getElementById('outPut').innerHTML= will never execute, run it infront of the return. Commented Oct 19, 2017 at 6:02
  • 1
    parseFloat... Commented Oct 19, 2017 at 6:04
  • 1
    extra '+' in after ditance Commented Oct 19, 2017 at 6:04
  • parseFloat and + after distance while setting innerHTML Commented Oct 19, 2017 at 6:05

2 Answers 2

2

Posting the corrected code.

There is always a good way to debug your code when writing front end code. The browser. Learn how to use browser console.

<!doctype html>
<!-- distance.html                     Mason Boroff -->
<!-- =================================================== -->
<html>
<head>
    <title> Distance Calculator </title>

    <script type="text/javascript">
    function ShowDistance()
        {
        var x1, x2, y1, y2;
            x1=parseFloat(document.getElementById('xOne').value);
            x2=parseFloat(document.getElementById('xTwo').value);
            y1=parseFloat(document.getElementById('yOne').value);
            y2=parseFloat(document.getElementById('yTwo').value);


        var distance = Math.sqrt( Math.pow((x1-x2), 2) + Math.pow((y1-
y2), 2) );


            document.getElementById('outPut').innerHTML=
                 'The distance bewtween (' + x1 + ',' + y1 + ') and (' 
+ x2 + ',' + y2 + ') is '+ distance ;
 return distance;
        }
    </script>
</head>

<body>
    <h2>Distance Calculator</h2>
    <p>
        Coordinate 1: (<input type="text" id="xOne" size=12 value=''> , 
<input type="text" id="yOne" size=12 value=''>)
        <br>
        Coordinate 2: (<input type="text" id="xTwo" size=12 value=''> , 
<input type="text" id="yTwo" size=12 value=''>)

        <br>
        <br>
        <input type="button" onclick="ShowDistance()" value="Calculate 
Distance">
    </p>
    <hr>
    <div id="outPut"> </div>
</body>

How I debugged it?

USe F12...then I looked into the console to check that there is an error in the line having extra +.

So I changed it.

Then I saw the complaign about parseFloat. Then I checked refernce to find out there is an method called parseFloat not parsefloat`.

Then I saw no outpout and no error. WHich made me check code again which revealed that there is return statement before you assign the result to innerHTML...

That's how I debugged it.

You should learn :-

  • How to use chrome debugger.
  • Checking the manual when you get stuck or in doubt about built in functions.
  • Learn a bit more javascript and basi control flow of programs.
Sign up to request clarification or add additional context in comments.

2 Comments

<input type="button" onclick="ShowDistance()" value="Calculate Distance";> there is also an error the ;behind is unecessary remove it pls
@Doomenik.: Yes I checked it.
-1

Run your code before posting a question.

function showDistance() {
  var x1 = (parseInt(document.getElementById('xOne').value)).toFixed(4);
  var x2 = (parseInt(document.getElementById('xTwo').value)).toFixed(4);
  var y1 = (parseInt(document.getElementById('yOne').value)).toFixed(4);
  var y2 = (parseInt(document.getElementById('yTwo').value)).toFixed(4);

  var distance = Math.sqrt( Math.pow((x1-x2), 2) + 
                 Math.pow((y1-y2), 2) );
       
  document.getElementById('outPut')
    .innerHTML= 'The distance bewtween (' + 
      x1 + ',' + y1 + ')  and (' + 
      x2 + ',' + y2 + ') is '+ distance;

    return distance;
}
<h2>Distance Calculator</h2>
<p>
Coordinate 1: (<input type="text" id="xOne" size=12 value=''> , 
<input type="text" id="yOne" size=12 value=''>)
<br>
Coordinate 2: (<input type="text" id="xTwo" size=12 value=''> , 
<input type="text" id="yTwo" size=12 value=''>)
<br>
<input type="button" onclick="showDistance()" value="Calculate 
Distance";>
</p>
<hr>
<div id="outPut"> </div>

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.