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 7 of 7
  1. #1
    New Coder
    Join Date
    Mar 2007
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JS beginner cant get code to work

    Hi..I am a 55 year old male who has decided I want to learn how to write JavaScript code. I have been doing online tutorials but I have run into a snag in a lesson. I have tried over and over to get this code to do what the author says it should but I am at a loss as to what I am doing wrong. i am typing it EXACTLY as the author but his code runs and mine does not. Please tell me what I am missing here.

    The authors code is supposed to write a series of grades and the appropriate letter grade for each student.


    <HTML>
    <HEAD>
    <TITLE>Grade Script</TITLE>
    <SCRIPT language="JavaScript">
    <!--
    function getGrade(name, grade){
    var letterGrade
    if(grade >= 90){
    letterGrade = "A"
    }
    if(grade >= 80 && grade < 90){
    letterGrade = "B"
    }
    if(grade >= 70 && grade < 80){
    letterGrade = "C"
    }
    if (grade < 70){
    letterGrade = "F"
    }
    document.write(name + " - " + letterGrade + " (" + grade + ")<BR>" )
    }
    //-->
    </SCRIPT>


    </HEAD>
    <BODY bgcolor="white">

    <P>
    <SCRIPT language="JavaScript">
    <!--
    getGrade("Mary", "94")
    getGrade("Jim", "70")
    getGrade("Albert", "65")
    getGrade("Carroll", "88")
    getGrade("Francis", "44")
    getGrade("George", "89")
    getGrade("Alice", "90")
    getGrade("Sam", "79")
    getGrade("Pat", "80")
    getGrade("Ray", "100")

    //-->
    </SCRIPT>

    </BODY>
    </HTML>


    The above code which was copy and pasted from the author runs properly.
    Below is the code I typed myself in NoteTab Pro. To me it is identical to the code above but it won't run. Is this some rookie keyboard issue per chance? I have only num lock on.


    <html>
    <head>
    <title>Grade Script</title>
    <SCRIPT language="JavaScript">
    <!--
    function getGrade(name, grade){
    var letterGrade
    if(grade >= 90){
    letterGrade = "A"
    }
    if(grade >= 80 && <90){
    letterGrade = "B"
    }
    if(grade >= 70 && <80){
    letterGrade = "B"
    }
    if(grade , 70){
    letterGrade = "C"
    }
    document.write(name + " - " + letterGrade + "("+ grade + ")<BR>")
    }
    //-->
    </SCRIPT>
    </head>
    <body bgcolor="white">
    <p>
    <script language="JavaScript" type="text/javascript">
    <!--
    getGrade("Mary", "94")
    getGrade("Jim", "70")
    getGrade("Albert", "65")
    getGrade("Carroll", "88")
    getGrade("Francis", "44")
    getGrade("George", "89")
    getGrade("Alice", "90")
    getGrade("Sam", "79")
    getGrade("Pat", "80")
    getGrade("Ray", "100")

    //-->
    </script></p>
    </body>
    </html>


    The desired output results of this simple script is to print the following:

    Mary - A (94)
    Jim - C (70)
    Albert - F (65)
    Carroll - B (88)
    Francis - F (44)
    George - B (89)
    Alice - A (90)
    Sam - C (79)
    Pat - B (80)
    Ray - A (100)

    I know this is a really basic problem to an advanced coder but I am just learning and have spent hours trying to figure the problem to no avail. Please can anyone shed some light on what I am doing wrong?
    Last edited by hy_tek; 12-10-2007 at 04:49 AM.

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    You need to look at what you typed again. It is not exactly like the code above.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    New Coder
    Join Date
    Mar 2007
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I changed some of the characters in the "document.write" line that were erroneous but I still can't get it to run. The fact that some of the script tags are in caps and some not doesn't really make any difference does it? NotTab uses lower case while the author used upper case. This is so frustrating. I have written and re written to no avail.

  • #4
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    The problem is in your if statements, you are leaving out an important thing after the &&.

    One thing that will make you life easier is to download Firefox [http://www.mozilla.com/en-US/firefox/] and install Firebug [http://www.getFirebug.com].

    Firebug is a great tool to help you debug JavaScript errors.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #5
    New Coder
    Join Date
    Mar 2007
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by A1ien51 View Post
    The problem is in your if statements, you are leaving out an important thing after the &&.

    One thing that will make you life easier is to download Firefox [http://www.mozilla.com/en-US/firefox/] and install Firebug [http://www.getFirebug.com].

    Firebug is a great tool to help you debug JavaScript errors.

    Eric
    Thank you so much I will try firebug....I need all the help I can get....

    I looked at the script that works and to my eyes the code is identical after the "&&" in the "if" statements. Bare with me here I am new at this and I don't know what I left out.
    Last edited by hy_tek; 12-13-2007 at 04:08 PM.

  • #6
    New Coder
    Join Date
    Dec 2007
    Posts
    16
    Thanks
    2
    Thanked 1 Time in 1 Post
    this is the copy and pasted statement from the author:

    if(grade >= 80 && grade < 90){

    this is your code:

    if(grade >= 80 && <90){

    there is nothing to compare in the second part of the if statement, ie nothing to be less than 90

    hope that helps

  • #7
    New Coder
    Join Date
    Mar 2007
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by aussiexile View Post
    this is the copy and pasted statement from the author:

    if(grade >= 80 && grade < 90){

    this is your code:

    if(grade >= 80 && <90){

    there is nothing to compare in the second part of the if statement, ie nothing to be less than 90

    hope that helps
    Wow how obvious was that? I should have seen that mistake. I think perhaps I was trying to fix it for so long that I couldn't see the forest through the trees. I read and reread that code over and over and I can't believe I missed that. Thank you so much for pointing it out to me.....I can sleep now....lol


  •  

    Posting Permissions

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