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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    javascript grade calculator

    PHP Code:
    [LEFT]<html>
    <
    body>
     <
    script type "text/javascript">
             <!--
             var 
    gradeCounter,  
                 
    gradeValue,    
                 
    total,         
                 
    average,       
                 
    grade;         
             
    total 0;        
             
    gradeCounter 0;  
             
    grade prompt("enter grade, -1 to Quit:""0" );
             
    gradeValue parseIntgrade );
             while ( 
    gradeValue != -) {
                
    total total gradeValue;
                
    gradeCounter gradeCounter 1;
                
    grade prompt"enter grade, -1 to Quit:""0" );
                
    gradeValue parseIntgrade );
             }
             if ( 
    gradeCounter != ) {
                
    average total gradeCounter;  
                
    document.write"<br>total grade: " gradeCounter "</bt>" );
                
    document.write"<br>average passing grade:" average "</br>");
             }
             else 
                
    document.write"total grade:"+);             
          
    </script>
          </body>
          </html>[/LEFT] 
    i'm writing this code for grade calculating, so far my code does the calculation as i wanted, however i try to get the code to actually show the inputs on the webpage like this

    PROMPTS OUTPUT in webpage
    “Please enter a grade” 85 85 Pass
    “Please enter a grade” 61 61 Fail
    “Please enter a grade” 95 95 Pass
    “Please enter a grade” -1 Total grades: 3. Average passing grade: 90.
    “Please enter a grade” -1 Total grades: 0.
    “Please enter a grade” 50 50 Fail
    “Please enter a grade” -1 Total grades: 1.

    so everytime i type in a grade, it will show on the webpage later.

    any help is appreciated. thank you.
    Last edited by tim656; 11-09-2012 at 05:19 PM.

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,112
    Thanks
    38
    Thanked 498 Times in 492 Posts
    Making some assumptions here base upon what I think are your requirements.
    Code:
    <html>
    <body>
    <div id="results"></div>
    <script type="text/javascript">
    var gradeCounter,  
        gradeValue,    
        total,         
        average,       
        grade;         
    var total = 0;        
    var gradeArray = [];
    
    grade = prompt("enter grade, -1 to Quit:", "0" );
    gradeValue = parseInt( grade );
    while ( gradeValue != -1 ) {
      total = total + gradeValue;
      gradeArray.push(gradeValue);
      grade = prompt( "enter grade, -1 to Quit:", "0" );
      gradeValue = parseInt( grade );
    }
    if ( gradeCounter != 0 ) {
      average = total / gradeArray.length;  
      str = 'Grades entered:<br>'+gradeArray.join('<br>');
      str += "<p>total grades entered: " + gradeArray.length;
      str += "<br>average passing grade:" + average + "<br>";
    } else str = "total grade: 0";             
    document.getElementById('results').innerHTML = str;
    </script>
    </body>
    </html>
    prompt() is not the best way to collect user information.
    For example:
    1. What effect does a wrong entry have on your calculations.
    2. How would user make a correction without starting ALL OVER again!!!
    3. How would handle an entry that the user wanted removed/

    Good Luck!

  • #3
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    can i use something else instead of var gradeArray = []; my professor haven't get to array yet.

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,112
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

    Quote Originally Posted by tim656 View Post
    can i use something else instead of var gradeArray = []; my professor haven't get to array yet.
    Don't use document.write() as it will reset your page after it is rendered (displayed).
    You can use it in your script because you don't render it until all data is collected, then displayed.
    You will need to save the inputs of the user somewhere.
    If you don't use an array, then you will need to save it to a continuous string, as in:
    Code:
    var str = '';
    ...
    while ...
    ...
      x = prompt(...)
      str += x+'<br>';  // if saved to an element with .innerHTM (as in earlier example)
    //  str += x+'\n';  // if saved to an <textarea> element with .value (little research needed here)
    ...
    }
    // save 'str' above to 'results' element or <textarea> if desired.
    ...

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    You should be using a form to collect the data - prompt is a debugging tool and in some browsers displays a checkbox for turning off JavaScript.

    You should be using innerHTML to display the output into a specified spot in the page.

    The best place to put JavaScript is immediately before the </body> tag where all of the HTML has already rendered and where it doesn't slow the loading of the page.

    Those are just about the first things that anyone learning JavaScript needs to know so if you are more than five minutes into a JavaScript course and the teacher hasn't mentioned those things yet then your teacher needs to take a JavaScript course themselves.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    well in my assignment, i'm only allow to use prompt() as input and document.write() to print results. Another question, how can i get my code to print a result like this

    85 pass
    61 fail
    95 pass
    total grades: 3. Average passing grade: 80.33333333

    so far my code only print this

    85
    61
    95
    total grades: 3. Average passing grade: 80.333333333333


  •  

    LinkBacks (?)


    Posting Permissions

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