+ Reply to Thread
Results 1 to 5 of 5

Thread: TAFE Student - Check my javascript.. line 31 int 40

  1. #1
    goldy30's Avatar
    goldy30 is offline x10Hosting Member goldy30 is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    60

    Question TAFE Student - Check my javascript.. line 31 int 40

    I thought I had it right but I've got an error where there is only text in a <td>... so have a look and tell me what ever is wrong with it.. as a whole.

    HTML Code:
    <HTML>
    <HEAD>
    <TITLE>Fuel Economy</TITLE>
    
    <script type="text/javascript">
    function updatesum()
    {
    if (document.form1.dist.value=="")
    {
    alert("Fill in the distance");
    }
    if (document.form1.fuel.value=="")
    {
    alert("Fill in the Fuel");
    }
    if (document.form1.cost.value=="")
    {
    alert("What is the cost of fuel?");
    }
    
    var dist = (document.form1.dist.value);
    var fuel = (document.form1.fuel.value);
    var cost = (document.form1.cost.value);
    
    var total = fuel*cost;
    var kmplt = dist/fuel;
    var ltpdist = fuel/dist * 100;
    var mpg = (dist * 0.621) / (fuel * 0.624);
    
    document.write("<center><table border="1" bgcolor="gray"><tr><td>Total cost</td><td>$" + total.toFixed(2) + "</td><td rowspan="4" valign="middle"><b>Calculate<br>Fuel Economy</b></td></tr>");
    document.write("<tr><td>Kilometers per litre used</td><td>" + kmplt.toFixed(2) + "</td></tr>");
    document.write("<tr><td>Fuel consumed</td><td>" + ltpdist.toFixed(2) + "</td></tr>");
    document.write("<tr><td>Miles per gallon travelled</td><td>" + mpg.toFixed(2) + "</td></tr></table></center>");
    }
    </script>
    
    </HEAD>
    
    <BODY bgcolor="lightblue">
    
    <center>
    
    <form name="form1">
    <table border="1" cellpadding="10" bgcolor="gray">
    
    <tr>
    <td>Distance traveled </td><td><input name="dist" size="5"> km</td><td rowspan="3" bgcolor="white"><center><font face="arial"><b>Calculate<br>Fuel Economy</b></font></center></td>
    </tr>
    <tr>
    <td>Fuel used </td><td><input name="fuel" size="5"> per litre</td>
    </tr>
    <tr>
    <td>Fuel cost </td><td><input name="cost" size="5"> per litre</td>
    </tr>
    
    <tr>
    <td colspan="3">
    <input type="button" value="Calculate" onClick="updatesum">
    <input type="reset" value="Reset">
    
    
    
    </td>
    </tr>
    </font>
    </table>
    
    </form>
    </center>
    
    </BODY>
    </HTML>
    I appreciate any help.

  2. #2
    Nahid_hossain is offline x10Hosting Member Nahid_hossain is an unknown quantity at this point
    Join Date
    Aug 2008
    Location
    Dhaka, Bangladesh
    Posts
    28

    Re: TAFE Student - Check my javascript.. line 31 int 40

    There are two mistakes.

    1. The code is
    <input type="button" value="Calculate" onClick="updatesum">
    will be
    <input type="button" value="Calculate" onClick="updatesum()">


    2.
    The code is
    HTML Code:
    document.write("<center><table border="1" bgcolor="gray"><tr><td>Total cost</td><td>$" + total.toFixed(2) + "</td><td rowspan="4" valign="middle"><b>Calculate<br>Fuel Economy</b></td></tr>");
    will be
    HTML Code:
    document.write("<center><table border=\"1\" bgcolor=\"gray\"><tr><td>Total cost</td><td>$" + total.toFixed(2) + "</td><td rowspan=\"4\" valign=\"middle\"><b>Calculate<br>Fuel Economy</b></td></tr>");
    Last edited by Nahid_hossain; 10-04-2008 at 08:35 AM.

  3. #3
    Scoochi2's Avatar
    Scoochi2 is offline x10 Sophmore Scoochi2 is an unknown quantity at this point
    Join Date
    Aug 2008
    Location
    Southport!
    Posts
    185

    Re: TAFE Student - Check my javascript.. line 31 int 40

    Quote Originally Posted by goldy30 View Post
    HTML Code:
    document.write("<center><table border="1" bgcolor="gray"><tr><td>Total cost</td><td>$" + total.toFixed(2) + "</td><td rowspan="4" valign="middle"><b>Calculate<br>Fuel Economy</b></td></tr>");
    An example of a major error is above. You're using double quotes for your document.write, but also for the HTML. When you use a double quote in the HTML, it ends our JS write...
    The easiest solution is to use single quotes. Either for the JS or the HTML within it- doesn't matter which.
    Or you could escape all the quotes which is the best way. So it should look like this:
    HTML Code:
    document.write("<center><table border=\"1\" bgcolor=\"gray\"><tr><td>Total cost</td><td>$" + total.toFixed(2) + "</td><td rowspan=\"4\" valign=\"middle\"><b>Calculate<br>Fuel Economy</b></td></tr>");
    You also have an error in the actual text. Should the 'fuel used' be xxx per litre??? If I drive a mile, I didn't use 0.1 per litres of fuel. I used 0.1 litre of fuel :p

    EDIT: meh. Took too long to reply :D
    Last edited by Scoochi2; 10-04-2008 at 08:49 AM. Reason: pipped to the post
    If anyone can see it, my post was meant for anyone who reads it. Don't take it personally or think I'm being condescending... :nuts:

  4. #4
    Nahid_hossain is offline x10Hosting Member Nahid_hossain is an unknown quantity at this point
    Join Date
    Aug 2008
    Location
    Dhaka, Bangladesh
    Posts
    28

    Re: TAFE Student - Check my javascript.. line 31 int 40

    I think some other things should also be fixed. I'm pasting full code here:

    HTML Code:
    <HTML>
    <HEAD>
    <TITLE>Fuel Economy</TITLE>
    
    <script type="text/javascript">
    function updatesum()
    {
    if (document.form1.dist.value=="")
    {
    alert("Fill in the distance");
    }
    else if (document.form1.fuel.value=="")
    {
    alert("Fill in the Fuel");
    }
    else if (document.form1.cost.value=="")
    {
    alert("What is the cost of fuel?");
    }
    else
    {
    
    var dist = (document.form1.dist.value);
    var fuel = (document.form1.fuel.value);
    var cost = (document.form1.cost.value);
    
    var total = fuel*cost;
    var kmplt = dist/fuel;
    var ltpdist = fuel/dist * 100;
    var mpg = (dist * 0.621) / (fuel * 0.624);
    
    document.write("<center><table border=\"1\" bgcolor=\"gray\"><tr><td>Total cost</td><td>$" + total.toFixed(2) + "</td><td rowspan=\"4\" valign=\"middle\"><b>Calculate<br>Fuel Economy</b></td></tr>");
    document.write("<tr><td>Kilometers per litre used</td><td>" + kmplt.toFixed(2) + "</td></tr>");
    document.write("<tr><td>Fuel consumed</td><td>" + ltpdist.toFixed(2) + "</td></tr>");
    document.write("<tr><td>Miles per gallon travelled</td><td>" + mpg.toFixed(2) + "</td></tr></table></center>");
    
    }}
    </script>
    
    </HEAD>
    
    <BODY bgcolor="lightblue">
    
    <center>
    
    <form name="form1">
    <table border="1" cellpadding="10" bgcolor="gray">
    
    <tr>
    <td>Distance traveled </td><td><input name="dist" size="5"> km</td><td rowspan="3" bgcolor="white"><center><font face="arial"><b>Calculate<br>Fuel Economy</b></font></center></td>
    </tr>
    <tr>
    <td>Fuel used </td><td><input name="fuel" size="5"> per litre</td>
    </tr>
    <tr>
    <td>Fuel cost </td><td><input name="cost" size="5"> per litre</td>
    </tr>
    
    <tr>
    <td colspan="3">
    <input type="button" value="Calculate" onClick="updatesum()">
    <input type="reset" value="Reset">
    
    
    
    </td>
    </tr>
    </font>
    </table>
    
    </form>
    </center>
    
    </BODY>
    </HTML>

  5. #5
    goldy30's Avatar
    goldy30 is offline x10Hosting Member goldy30 is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    60

    Re: TAFE Student - Check my javascript.. line 31 int 40

    Thanks once again! I understand and have noted the errors I've made, I've corrected the mistakes and it all works properly now.

    You guys are great..... Thanx.

+ Reply to Thread

Similar Threads

  1. Replies: 14
    Last Post: 09-29-2008, 07:07 PM
  2. It works! ...
    By bpakidz in forum Programming Help
    Replies: 4
    Last Post: 09-07-2008, 11:12 PM
  3. New Site-Suggestions?
    By mnoutside in forum Review My Site
    Replies: 9
    Last Post: 08-27-2008, 07:01 AM
  4. GeIP Script Help
    By cybahq in forum Scripts & 3rd Party Apps
    Replies: 4
    Last Post: 07-30-2008, 06:19 AM
  5. perl script for guestbook, need help
    By shifeng in forum Scripts & 3rd Party Apps
    Replies: 14
    Last Post: 01-06-2008, 12:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers