/    Sign up×
Community /Pin to ProfileBookmark

Help with my looping assignment

I would usually ask a friend to help me who is good with javascript, but he’s unavailable and I’m pressed for time. It’s for my intro to programming class, and I got most of it..I think..let me just give you the prompt.

“Alberta Einstein teaches a business class at Podunk University. To evaluate the students in this class, she has given three tests. It is now the end of the semester and Alberta would like you to design a program, that inputs each student’s test scores and outputs the average score for each student and the overall class average.”

Ok, so I got it to input and output each individual student’s name and average, but I have no clue how to make it take the class average. Every attempt I have made for the past 2 hours have only taken the average of the last calculated student’s average and divided it by the total number of students entered in the class.

I was hoping you guys could take a quick look at my code and tell me what I’m missing to do the class average. I’m sure it’s something simple that just sliped my mind, but like I said, I have to turn this assignment in within 11 hours of now. (It’s 1:30 here.)

Now for my current code:

[CODE]
<script type =”text/javascript”>

//Lab 5-B
//Name
//11.03.05
//Class Average Program
//This program will create a class average based
//on 3 tests entered per student in the class.

//create the input function
function Input()
{
var counter = 0
var average = 0
var students = parseInt(prompt(“How many students are in the class?”, “”))

//create loop
while (counter++ < students)
{
var studentname = String(prompt(“Enter Student ” + counter + “‘s name.”,””))

var test1 = parseFloat(prompt(“Enter ” + studentname + “‘s first test score.”,””))
var test2 = parseFloat(prompt(“Enter ” + studentname + “‘s second test score.”,””))
var test3 = parseFloat(prompt(“Enter ” + studentname + “‘s third test score.”,””))
average = (test1 + test2 + test3) / 3
document.write(studentname + “‘s average score is ” + average + “.<br>”)
}
}
function Output()
{
classaverage = average / students
document.write(“The class average is: ” + classaverage + “.”)
}
var classaverage

Input()
Output()

</script>
[/CODE]

Thanks in advance! ?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@LiphotoNov 04.2005 — i am a bigginer too so i hope i am not wasting your time. I am not sure about the codding but this is what i would do.

return then add all the averages of all the students i have dealt with. then i would find the average of that sum i have just calculate to get the average of the whole class.
Copy linkTweet thisAlerts:
@KorNov 04.2005 — It is easy, but you must use a 2nd array for that calculation. And you must take care not to add each student's average but to add each student's test value for the general average.

Here's a dynamic and general aproach:
[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function calc(){
var nrstud = parseInt(prompt('How many students are in the class?', ''));
var nrtest =parseInt(prompt('How many tests are per student?', ''));
var root1 = document.getElementById('sa');
var root2 = document.getElementById('ga');
var rez =[];
var ssum=[];
var gsum=0;
for(var i=0;i<nrstud;i++){
rez[i] =[];ssum[i]=0;
for(var j=0;j<nrtest;j++){
rez[i][j] = prompt('Enter test '+(j+1)+' for student '+(i+1),'')
ssum[i]+=Number(rez[i][j])
}
var save = (ssum[i]/nrtest).toFixed(2);
var oDiv = document.createElement('div');
oDiv.appendChild(document.createTextNode('Student '+(i+1)+' average: '+save))
root1.appendChild(oDiv)
gsum+=ssum[i];
}
var gave = (gsum/(nrtest*nrstud)).toFixed(2)
var oDiv = document.createElement('div');
oDiv.appendChild(document.createTextNode('Class average: '+gave))
root2.appendChild(oDiv)
}
function res(){
document.getElementById('sa').innerHTML='';
document.getElementById('ga').innerHTML='';
calc();
}
onload=calc;
</script>
</head>
<body>
STUDENTS' AVERAGE
<div id="sa"></div>
CLASS AVERAGE
<div id="ga"></div>
<br>
<br>
<input type="button" value="Start a new calculation" onclick="res()">
</body>
</html>
[/code]


Fell free to ask me if you don't undersatand the code.
Copy linkTweet thisAlerts:
@KorNov 04.2005 — 
add all the averages of all the students i have dealt with
[/quote]

Nope. When calculate the average for each student, there might be sometime some rounds here and there. When adding rounded numbers and dividing again this small rounds might compound into errors. The general average is not the sum of the partial avarage, it is simply the general average. Add again all the tests and divide to the total numbers of all the tests.
Copy linkTweet thisAlerts:
@KorNov 04.2005 — And here's a variant where u can enter the student's names as well:
[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function calc(){
var nrstud = parseInt(prompt('How many students are in the class?', ''));
var nrtest =parseInt(prompt('How many tests are per student?', ''));
var root1 = document.getElementById('sa');
var root2 = document.getElementById('ga');
var rez =[];
var ssum=[];
var gsum=0;
for(var i=0;i<nrstud;i++){
rez[i] =[];ssum[i]=0;
var stud = prompt('Enter name of the student '+(i+1), '')
for(var j=0;j<nrtest;j++){
rez[i][j] = prompt('Enter test '+(j+1)+' for student '+stud,'')
ssum[i]+=Number(rez[i][j])
}
var save = (ssum[i]/nrtest).toFixed(2);
var oDiv = document.createElement('div');
oDiv.appendChild(document.createTextNode(stud+' average: '+save))
root1.appendChild(oDiv)
gsum+=ssum[i];
}
var gave = (gsum/(nrtest*nrstud)).toFixed(2)
var oDiv = document.createElement('div');
oDiv.appendChild(document.createTextNode('Class average: '+gave))
root2.appendChild(oDiv)
}
function res(){
document.getElementById('sa').innerHTML='';
document.getElementById('ga').innerHTML='';
calc();
}
onload=calc;
</script>
</head>
<body>
STUDENTS' AVERAGE
<div id="sa"></div>
CLASS AVERAGE
<div id="ga"></div>
<br>
<br>
<input type="button" value="Start a new calculation" onclick="res()">
</body>
</html>
[/code]
×

Success!

Help @ton spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...