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 10 of 10

Thread: Newbie Question

  1. #1
    New Coder
    Join Date
    Jun 2010
    Location
    London
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Newbie Question

    Hi Everyone

    I'm learning Javascript with varying degree's of success but am very much in the early phase.
    My question/problem is this:


    [ICODE]<!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>
    <script language="javascript" type="text/javascript">



    var degFahren = new Array(212, "string data", -459.67);
    var degCent = new Array();
    var loopCounter;

    for(loopcounter =0; loopCounter <= 2; loopCounter++){
    if(isNaN(degFahren[loopCounter])){
    alert("Data " + degFahren[loopCounter] + " at array index " +
    loopCounter + " is invalid");
    continue;
    }
    degcent[loopCounter] = 5/9 * (degFahren[loopCounter] -32);
    }
    for (loopCounter =2; loopCounter >=0; loopCounter --){
    document.write("Value " + loopCounter + " was " + degFahren[loopCounter] +
    " degrees Fahrenheit");
    document.write(" which is " + degCent[loopCounter] +
    " degrees centigrade<br />");
    }
    </script>

    </body>
    </html>
    /ICODE]

    The following code does not return any errors in my browser but I'm confused since I'm sure I should be seeing an alert box telling me that there is invalid data entered bu this doesn't happen, also as far as I can make out I should only be receiving an error for the 2nd loop but there seem to be an error message returned for all 3 loops??

    This is all possibly correct but I'm learning from a book and I have no-one to ask these mundane questions to.

    Any help would be greatly received.

    Sara

  • #2
    New Coder
    Join Date
    Jun 2010
    Location
    London
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    And if some-one could tell me where I'm going wrong with the code tags I'll be able to improve the presentation of any future posts.



  • #3
    Senior Coder effpeetee's Avatar
    Join Date
    Feb 2007
    Location
    Clapham Junction - London SW
    Posts
    4,885
    Thanks
    228
    Thanked 204 Times in 203 Posts
    Try this site for help.

    JSLint expects the whole page code including the HTML

    Frank
    Last edited by effpeetee; 06-14-2010 at 09:48 AM.
    * Sources (updated: 21.11.2012.
    Using Windows 8 Professional. 64bit with HP Photosmart 5510 printer Very useful site here.

  • #4
    New Coder
    Join Date
    Jun 2010
    Location
    London
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I'm sorry I thought this was a place to come for help.

    Thanks...I guess

  • #5
    Senior Coder effpeetee's Avatar
    Join Date
    Feb 2007
    Location
    Clapham Junction - London SW
    Posts
    4,885
    Thanks
    228
    Thanked 204 Times in 203 Posts
    Quote Originally Posted by sara jane View Post
    I'm sorry I thought this was a place to come for help.

    Thanks...I guess
    No, please JSLint is offered for your help. It validates the javascript. It is the best that I can do. Also, any answer to you will keep your post alive.

    There are many others here who can help you.

    Frank
    * Sources (updated: 21.11.2012.
    Using Windows 8 Professional. 64bit with HP Photosmart 5510 printer Very useful site here.

  • #6
    New Coder
    Join Date
    Jun 2010
    Location
    London
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hi,

    Ok Frank, I've bookmarked that site and appreciate your help.
    I have the web developer tool installed on firefox and will run a few test's to see if there are differences between the two.
    I suppose I was just hoping some wise guru would be able to just sum up the problem in a line or 2.
    As I said before according to my own debugger there are no errors in the code and I just wondered if the alert box doesnt always mean you will receive one, particularly as the code is run with an invalid number before the alert() function.

    Again thanks for the link.

  • #7
    Senior Coder coothead's Avatar
    Join Date
    Jan 2004
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,680
    Thanks
    0
    Thanked 230 Times in 225 Posts
    Hi there sara jane,

    and a warm welcome to these forums.

    you have a couple of typing errors in your code....
    Code:
    
    <!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>
    
    <!--remove "language="javascript" it is deprecated -->
    <script language="javascript" type="text/javascript">
    
       var degFahren=new Array(212,"string data",-459.67);
       var degCent=new Array();
       var loopCounter;
    
    //the "c" in first instance of "loopcounter=0" should be upperCase
    for(loopcounter=0; loopCounter<=2;loopCounter++){
    if(isNaN(degFahren[loopCounter])){
       alert("Data "+degFahren[loopCounter]+" at array index "+loopCounter+" is invalid");
       continue;
    }
    //the "c" in "degcent[loopCounter]" should be upperCase
    degcent[loopCounter]=5/9*(degFahren[loopCounter]-32); 
    }
    for(loopCounter=2;loopCounter>=0;loopCounter--){
    document.write("Value "+loopCounter+" was "+ degFahren[loopCounter]+" degrees Fahrenheit");
    document.write(" which is "+degCent[loopCounter]+" degrees centigrade<br />");
    }
    </script>
    
    </head>
    
    <body>
    
    </body>
    </html>
    
    Make the changes and it should work OK.

    coothead

  • Users who have thanked coothead for this post:

    sara jane (06-14-2010)

  • #8
    New Coder
    Join Date
    Jun 2010
    Location
    London
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hi coothead,

    Works great, thanks alot slightly embarrassed by the the typing errors and in future if I post again I will double check for these first.

    Thanks again

  • #9
    Senior Coder coothead's Avatar
    Join Date
    Jan 2004
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,680
    Thanks
    0
    Thanked 230 Times in 225 Posts
    No problem, you're very welcome.

    p.s. Dno't feel ebmassrared by tpynig eorrrs, we all mkae tehm.

  • #10
    Senior Coder effpeetee's Avatar
    Join Date
    Feb 2007
    Location
    Clapham Junction - London SW
    Posts
    4,885
    Thanks
    228
    Thanked 204 Times in 203 Posts
    Quote Originally Posted by coothead View Post
    No problem, you're very welcome.

    p.s. Dno't feel ebmassrared by tpynig eorrrs, we all mkae tehm.
    Noonsens. eye nevre macke tiepin ore splelign erorrs. like. Innit?

    Knarf
    * Sources (updated: 21.11.2012.
    Using Windows 8 Professional. 64bit with HP Photosmart 5510 printer Very useful site here.


  •  

    Posting Permissions

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