Click here to Skip to main content
16,021,115 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
1st piece of code allows me to use an alert to compile a list of grades:


Add a grade!





var grades = [10,12,13];

function loadGrades(){
document.getElementById("grades").innerHTML =grades;

}
function myFunction() {
var grade = prompt("what is the next grade?");
grades[grades.length]=grade;

document.getElementById("grades").innerHTML = grades;
}




2nd piece of code allows me to calculate the mean of a list of numbers but only when I manipulate the list from within the code:





var Grades = [10,20];

var sum=0;
if(Grades.length>0){
for(index=0;index<Grades.length;index++){

sum+=Grades[index];
}


document.write(sum/Grades.length);

document.write(" is the average of the following grades" + Grades);
}
else
document.write("Emptyy");




I cannot figure out how to make the new list (when I enter a new number in the prompt) to automatically update the mean of the grades. I would have thought it would have been as easy as cutting and pasting the two parts together but I couldn't get it to work.

Any thoughts would be appreciated!

Thanks.

What I have tried:

I tried pasting the two parts together in a number of different ways but nothing worked.
Posted
Updated 12-Oct-19 5:12am

1 solution

Quote:
I tried pasting the two parts together in a number of different ways but nothing worked.

Why on Earth would you assume that?
Would you assume that if you glued half a Ford Transit to half a Bugatti Veyron you'd get a van that did 250 mph, could carry 2 ton of bricks, and got 50mpg?

You've taken two very different pieces of code that happen to use the same language (the fast pedal is on the right, the slow one iis next to it) and some of the same names - then glued them together and hoped that will pass as "doing your homework". When it doesn't work, instead of thinking "what did I do wrong" you've come here and asked us.

Well, I'll tell you: you are glueing two halves of unrelated vehicles together. That may work occasionally, but will fail nearly every time ...

Throw the whole load away, read your homework question again, and try to work out how to solve it properly - it'll be a lot quicker, easier, and simpler in the long run, and will produce better code, which probably means a better grade ...
 
Share this answer
 
Comments
Luc Pattyn 12-Oct-19 11:23am    
2 ton of bricks? What happened to your 3D printer plans?
[no name] 12-Oct-19 11:48am    
How do you think he made the bricks?
OriginalGriff 12-Oct-19 12:04pm    
I'm printing the bricks:
http://www.commitstrip.com/en/2017/12/08/stack-overflow-patchwork/
Luc Pattyn 12-Oct-19 12:09pm    
Ah, so I'm glad to see you are getting the output you think you should be getting...

;)
mattkrebs 12-Oct-19 12:15pm    
This comment ("instead of thinking "what did I do wrong" you've come here and asked us") makes me look like I am lazy.

I don't know how (with my question) I left the impression that I did not think for some time "What did I do wrong?" I did infact do this for some time. And I tinkered with it for some time. Obviously I was unsuccessful.

Can someone please advise me how I may in the future submit questions in such a way that I don't give off the impression that I am being lazy or that I did not put in the appropriate amount of "struggle" myself prior to submitting a question?

I am not looking to waste anyone's time. I am just hoping to learn how to code.

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