Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy New to JavaScript...

    Greetings Everyone. I recently started a Web Programming class in school and need to come with a JavaScript code, that converts Fahrenheit to Celsius. And display some quiry message depending on the temperature. While I feel I am on the right track, when you use this "converter" the Celsius numbers come up Not a Number (NaN) I am in no way asking someone to do this for me, I am merely asking for someone with more experience take a look at it and point me in the right direction. I will post the code below. Thanks.

    -MadAsRabbits AKA Eddie



    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

    <body>
    <input type="text" id="fTemp"=>degrees Fahrenheit</input>
    <button onclick="test()">Submit</button>
    <script>

    function test()
    {
    var fTemp = document.getElementById("fTemp").value;
    document.write("<p>" + fTemp
    + " Fahrenheit is equal to " + cTemp
    + " degrees Celsius.</p>");
    }

    var cTemp = (fTemp - 32) * (5 / 9);

    if (fTemp<70)
    {
    document.write("It is a little cool today")
    }
    else if (fTemp>70)
    {
    document.write("It is a nice day today")
    }
    </script>

    </body>


    </html>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,252
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    Use your error console (F12 key) to expose syntax errors. You may not have line breaks in your document.write(). cTemp is NaN because you try to display the result before you have performed the calculation.

    Code:
    <input type="text" id="fTemp"=> degrees Fahrenheit</input>
    <button onclick="test()">Submit</button>
     
    <script type = "text/javascript">
    
    function test() {
    var fTemp = document.getElementById("fTemp").value;
    var cTemp = (fTemp - 32) * (5 / 9);
    cTemp = cTemp.toFixed(2);  // 2 decimal places
    document.write(fTemp + " degrees Fahrenheit is equal to " + cTemp + " degrees Celsius. <br>");
    
    if (fTemp<70) {
    document.write("It\'s a  little cool today");  // how to include an apostrophe
    }
    else if (fTemp>=70) {  // note >=
    document.write("It\'s a nice day today");
    } 
    
    }
    
     </script>

    You may want to extend the messages to cover very high/low temperatures! As it is there are no limits as to what the user may enter.

    document.write() has been regarded as obsolete since Netscape3 passed away 10+ years ago.. Use DOM methods to display a message to your users.

    Why are you using XHTML?

    It is recommended that the opening brace { is placed on the same line as the function, if, else, do, while, or for statement and not on the following line. This is because of something known as automatic semi-colon insertion. If you disregard this advice it is looking out for its chance and one day it will rise up and bite you in the undercarriage, and create an error which can be hard to find.

    Do please read the posting guidelines regarding silly thread titles. The thread title is supposed to help people who have a similar problem in future. Yours is useless for this purpose.
    Last edited by Philip M; 08-06-2014 at 10:10 AM. Reason: Typo

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    MadAsRabbits (08-06-2014)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •