Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-11-2012, 02:40 AM   PM User | #1
EricaBrachman
New to the CF scene

 
Join Date: Oct 2012
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
EricaBrachman is an unknown quantity at this point
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>
EricaBrachman is offline   Reply With Quote
Old 10-11-2012, 02:49 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,229
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Old 10-11-2012, 02:52 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,229
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Old 10-11-2012, 03:06 AM   PM User | #4
EricaBrachman
New to the CF scene

 
Join Date: Oct 2012
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
EricaBrachman is an unknown quantity at this point
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.
EricaBrachman is offline   Reply With Quote
Old 10-11-2012, 03:09 AM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,229
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Users who have thanked Old Pedant for this post:
EricaBrachman (10-19-2012)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:14 AM.


Advertisement
Log in to turn off these ads.