473,406 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Problem to combine 2 separate scripts properly

1st piece of code allows me to use an alert to compile a list of grades:

Expand|Select|Wrap|Line Numbers
  1. <body onload="loadGrades()">
  2. <button onclick="myFunction()">Add a grade!</button>
  3. <p id="grades"></p>
  4.  
  5.  
  6. <script>
  7. var grades = [10,12,13];
  8.  
  9. function loadGrades(){
  10. document.getElementById("grades").innerHTML =grades;
  11.  
  12. }
  13. function myFunction() {
  14. var grade = prompt("what is the next grade?");
  15. grades[grades.length]=grade;
  16.  
  17. document.getElementById("grades").innerHTML = grades;
  18. }
  19.  
  20. </script>
  21. </body>
  22.  
  23.  
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:


Expand|Select|Wrap|Line Numbers
  1.  
  2. <script>
  3.  
  4.  
  5. var Grades = [10,20];
  6.  
  7. var sum=0;
  8. if(Grades.length>0){
  9. for(index=0;index<Grades.length;index++){
  10.  
  11. sum+=Grades[index];
  12. }
  13.  
  14.  
  15. document.write(sum/Grades.length);
  16.  
  17. document.write(" is the average of the following grades" + Grades);
  18. }
  19. else
  20. document.write("Emptyy");
  21.  
  22.  
  23. </script>
  24.  
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 wouild be appreciated!

Thanks.
Oct 12 '19 #1
1 1591
gits
5,390 Expert Mod 4TB
well first the array that contains the numbers for your calculation in the 2 script-snippets have different names - thus they are independent from each other when you just want to merge the snippets. But it wouldn't work either then - because you would need to recalculate when the input was made and the array was updated. So wrap the calculation and output of the result into another function as well and call it at the right place.
Oct 14 '19 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Colin Colin | last post by:
I downloaded a calendar.asp file that someone named Jacob "WickedPisser" Gilley made. It's a few years old but It works fine and it's what I was looking for without getting into ActiveX objects. ...
9
by: LNM | last post by:
I am trying to create a survey type form where multiple choice answers are selected (radios) and I am using an onclick event to update a text box with 'correct', etc. I am using the...
9
by: beguigne | last post by:
Below is a snippet of a crude date picking routine for a form. I am a novice at this so, I am hitting my head at why when I select the day, the onChange event gives me a blank. What am I missing?...
7
by: e | last post by:
I've been having an extremely difficult time finding an answer to this in IE / js groups, so I thought I'd try here. I've got an aspx page that delivers loads of report data into custom-named...
12
by: Joe | last post by:
Hello All: I'm sure that one of you can quickly spot the error in my script. I am using a small javascript function to retrieve the text of a hyperlink in a datagrid column. Here is my...
1
by: number1.email | last post by:
Hello, I have a simple Web Page Questionairre in which questions are read from a database, and the user can indicate the correct answer via either a radio input control or a dropdown list. The...
4
by: Joey | last post by:
I am going to use a collection of either div HTML server controls or panel web server controls (renders as div on newer browsers) on a web form. I need to be able to hide them with a client-side...
17
by: Man-wai Chang | last post by:
I have a form formABC that has a textbox plus a button. On clicking the button, window.open() a popup window for user to select a value. The value will then be passed back to formABC's textbox. ...
1
by: Daniel Kaplan | last post by:
So just started playing with Javascript, and my first run at making three step buttons came out just fine. But I do have a general question. In my book's example in the HTML script the IMG tags...
3
by: bnsmith001 | last post by:
I have a text file with 29766 words in it. Currently, the file is formatted with one word per line, followed by a DOS CR/LF. I'd like to write a Word-of-the-Day tool solely in javascript, where I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.