Click here to Skip to main content
15,666,844 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to create a grade calculator in js using the requirements (in comments), but I'm stuck. I've made some progress, after reviewing some work and incorporating some useful feedback.

How can I get the program to ask for each grade and then stop after all grades are entered?

How do I get it to push each grade into the array (I have one push statement but I don't know how this works when the grades are unknown at this point).

Thanks in advance.

What I have tried:

var grades = [];

document.writeln("Welcome to the grade calculator!");

var numClasses = prompt("How many classes are you taking?");

//use a for loop to iterate, one for each class. Prompt for their grade. Store the response in the array grades. Use push method to add the new item to the array.

for (var i = 0; i < numClasses.length; i++){
console.log (numClasses[i]);
var question = prompt ("What was your grade in class " + i + "?");


//Once all of the grades have been entered and stored in the array, calculate the average grade. First use another for loop to calculate the sum of all grades, then dividing that sum by the number of classes to get the average.

for ( var j = 0; j < grades.length; j ++){
    var avg = (grades[j] / grades.length) * grades.length

if (avg < 60) {
document.writeln("Keep studying! Review your notes and work every night.");

else if (avg >=60 && < 80) {
document.writeln("Be sure to review your work; you can do it!");
else {
document.writeln("Way to go! Keep up the good work."):
Updated 19-Oct-16 12:18pm
[no name] 19-Oct-16 11:49am    
The semicolon at the end of your for loop is going to cause your for loop to basically do nothing.
[no name] 19-Oct-16 13:45pm    
Ah now I see the actual question, "//I'm stuck on how to write a for loop to calculate the average." In the future do not embed your question in comments in the code. It's makes things so much harder.

Okay so why can't you do a for loop? Loop through your grades array and add up all the grades then divide by the number of grades to get an average. You wrote one for loop already another one isn't really all that different.
Member 12800921 19-Oct-16 14:14pm    
Thanks. I updated the code, but I know there's still stuff that's not working.
[no name] 19-Oct-16 14:21pm    
Yes I see that. Unfortunately, you keep changing the target so your posting is going to have several different answers and we can't teach you programming in a forum posting.

Bug report:
var question = prompt (What was your grade in class " + i + "?")
var question = prompt ("What was your grade in class " + i + "?");
Share this answer
Member 12800921 19-Oct-16 18:18pm    
Thanks, fixed it.
Start with JavaScript Tutorial[^].
Share this answer
Member 12800921 19-Oct-16 13:09pm    
Yeah, I've done those. :)
Richard MacCutchan 19-Oct-16 13:32pm    
Then you should be able to do the rest of your homework assignment yourself.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900