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 to the CF scene
    Join Date
    Apr 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    function/button not working correctly?

    I read that you don't allow homework assignments to be posted, but this is only one small section of the assignment (1 page of 5), and I have a migraine, and I am utterly hopeless and lost (ha)...

    I was wondering if there was any errors you could point out to me that would explain why the button isn't doing ANYTHING (i.e. not calculating the sum, average, or grade) when i click it. I'm new to js so I'm sure it could be pretty much any n00b error in the function code.

    But like I said any help would be greatly appreciated...

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
          "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 
    
    <head>
      
    <link href="5A.css" rel="stylesheet" type="text/css" media="screen"/>
    
    
    
    <script type="text/javascript">
    
    function DisplayGrades()
    { var aNum= 1* document.getElementById("txtaNum").value;
      var bNum= 1* document.getElementById("txtbNum").value;
      var Avg = (aNum+bNum)/2; document.getElementById("txtAvg").value=Avg;
      var Sum= aNum+bNum; document.getElementById("txtSum").value=Sum;
      var alphaGr= document.getElementbyID("txtalphaGr").value=alphaGr;
    
    if (Avg <=  60)
    
             alphaGr="F";
    
     if (Avg>60 && Avg <= 70)
    
            alphaGr="D";
    
     if (Avg>70 && Avg <= 80)
    	alphaGr="C";
    
     if (Avg>80 && Avg <=90)
    	alphaGr="B";
    
     if (Avg>90 && Avg <=100)
    	alphaGr="A";}
    
    </script>
    </head>
    <body>
    <div id="wrapper">
    
      <div id="interact">
    Enter your grade for Test 1: <input type="text" id=txtaNum" class="controls" size="10" /> <br />
    
    Enter your grade for Test 2: <input type="text" id=txtbNum" class="controls" size="10" /> <br />
    
    <input type="button" id="btnDisplayGrades" value="Display Grades" onclick="DisplayGrades();" />
    
    <hr/>
    
    Sum of Your Grades: <input type="text" id="txtSum" class="controls" size="10" readonly="readonly" /> <br/>
    
    Average Grade: <input type="text" id="textAvg" class="controls" size="10" readonly="readonly" /> <br />
    
    Alpha Grade: <input type="text" id="txtalphaGr" class="controls" size="10" readonly="readonly" />
    
      </div>
    </div>
    </body>
    </html>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,542
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    How hard will you kick yourself??

    Code:
    Enter your grade for Test 1: <input type="text" id=txtaNum" class="controls" size="10" /> <br />
    Enter your grade for Test 2: <input type="text" id=txtbNum" class="controls" size="10" /> <br />
    Average Grade: <input type="text" id="textAvg" class="controls" size="10" readonly="readonly" /> <br />
    Don't strings have quote marks on BOTH ENDS?

    And check out the spelling of "textAvg". Is it right?

    I'll let you figure out what's wrong with your letter grade code. But here's a hint: JavaScript can't do time travel.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,542
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    By the by, if you would use FireFox and install the FireBug plug in and learn to use it, you'd find things like this instantly.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,542
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    And by the way, when people do this much work before asking questions, most anybody here will be happy to help. It's only when somebody just posts the assignment exactly as it came from the instructor with not even making a start at it that we get anal retentive.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    gahhhh the answer to your question is... hard, right in the face!

    thank you BIG TIME for pointing out the errors and the advice. I am installing it as we speak

    As for the riddle..." js can't do time travel".. I am still at a loss :-/

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,542
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    Heh!

    Okay, another clue.

    You can't *USE* the value of a variable *BEFORE* you compute the value of the variable.

    You are computing the letter grade *AFTER* you try to stuff it in place. Asking JS to predict what will be in the variable in the future. Okay, it's only a few nanoseconds in the future, but it's still the future. <grin/>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think i understand what you are saying... :-/

    That ONE button I have is suppose to calculate all 3 fields...

    The only way I know to fix the "time travel" problem (if i'm understanding correctly) is to add another button, and for my assignment I am not allowed to do so.

    my head hurts too bad to even think

    but thanks for your help.


  •  

    Posting Permissions

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