...

View Full Version : javascript grade calculator



tim656
11-09-2012, 05:16 PM
<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 = parseInt( grade );
while ( gradeValue != -1 ) {
total = total + gradeValue;
gradeCounter = gradeCounter + 1;
grade = prompt( "enter grade, -1 to Quit:", "0" );
gradeValue = parseInt( grade );
}
if ( gradeCounter != 0 ) {
average = total / gradeCounter;
document.write( "<br>total grade: " + gradeCounter + "</bt>" );
document.write( "<br>average passing grade:" + average + "</br>");
}
else
document.write( "total grade:"+0 );
</script>
</body>
</html>

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.

jmrker
11-09-2012, 06:52 PM
Making some assumptions here base upon what I think are your requirements.


<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!

tim656
11-09-2012, 09:00 PM
can i use something else instead of var gradeArray = []; my professor haven't get to array yet.

jmrker
11-09-2012, 09:44 PM
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:


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.
...

felgall
11-09-2012, 11:12 PM
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.

tim656
11-11-2012, 01:29 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum