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

    Entering Student Grades

    I'm supposed to be coding a web page to where you enter three grades for a student, and then display the average and the letter grade. I did this part fine and now I am to add nested loops so that I can add three grades from four different students and have their individual info displayed.

    I think just a nudge in the right direction is all I need. Here's the code I have.

    Code:
    <html>
    <body>
    <script type="text/javascript">
    
    
    // Variables and Constants
    var arrGrades = new Array()
    var quantity = 3;              // number of grades to be entered
    var total = 0;                 
    var lGrade = '';
    
    // Prompt user for grades
    for (var i=0; i<quantity; i++)
    arrGrades[i] = prompt("Enter grade number "+(i+1),"");
    
    // Write entered grade data to page and calculate total
    for (var i in arrGrades) {
    document.write("Grade "+(parseInt(i)+1)+" = "+arrGrades[i]+"<br>");
    total += parseInt(arrGrades[i]);
    }
    
    // Calculate average, rounded
    var average = Math.round(total/arrGrades.length);
    
    // Determine letter grade
    if (average >= 90)
    lGrade = 'A';
    else if (average >= 80)
    lGrade = 'B';
    else if (average >= 70)
    lGrade = 'C';
    else if (average >= 60)
    lGrade = 'D';
    else
    lGrade = 'F';
    
    // Write result to page
    document.write("<br>Your average is "+average+". You have a "+lGrade);
    </script>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,536
    Thanks
    80
    Thanked 4,490 Times in 4,454 Posts
    Unless the requirement is to remember all data for all students and then dump all the results out at the end, you don't really need to change much.

    You could just put a for loop around *ALL* of the existing code.

    Maybe add a prompt for student name at the top of the loop and then dump out the name as part of printing their average.

    Code:
    for ( var st = 1; st <= 4; ++st )
    {
         var student = prompt("Enter the name of student #" + st);
         ... your existing code ...
    
        // Write result to page...NEXT LINE IS CHANGED:
        document.write("<br>" + student + "'s average is "+average+". " 
                                  + student + " hase a "+lGrade + "<hr/>");
    }
    Why work harder?

    Now...if you have to remember *ALL* the data and write it all out at the end you'll have to do a bit more work.
    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
    26,536
    Thanks
    80
    Thanked 4,490 Times in 4,454 Posts
    By the way: prompt() and document.write() are considered obsolete. Nobody would use them for any real JavaScript code. Your book/instructor is (as Felgall would say) teaching you the history of JavaScript, not modern JavaScript coding.
    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
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Yeah, all of the data is supposed to be remembered and displayed.

    I've definitely noticed looking online that some of the things in the book are now different than things I am seeing or would be better to use now. I think that's part of what's making me have so much trouble. You would think they would try and keep things as current as possible in this field.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,536
    Thanks
    80
    Thanked 4,490 Times in 4,454 Posts
    Okay, to remember the data, just add arrays:

    Code:
    var students = [ ];
    var averages = [ ];
    var grades = [ ];
    As you go through the loop on st that I showed, put the name of the student into students[st] and the average for the student into averages[st] and the letter grades into grades[st].

    Don't overthink it.
    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.

  • Users who have thanked Old Pedant for this post:

    EricaBrachman (10-19-2012)


  •  

    Posting Permissions

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