Find The Average Of The Values In An Array Of Numbers Passed As Argument To A Function?
Apr 20, 2009
How do i use a function to find the average of the values in an array of numbers passed as argument to a function.(using java script) thank you for your help...
New to JS. Have ordered some new books, but need to get somethings going in the mean time. What I wanted to do is to link to a new page having a date range input in the form of 6 text boxes, 2 sets of mm-dd-yy, from and to, where the upon loading the page box 1 of the from would auto focus and then auto tab and then post where php could take over on the server side. Setting up the form and the lay out, no problem. Auto focusing went just like expected using the following code, included because on the next step everything fell apart.
So then the wheels came off and in response, began to simplify what was trying to be done to find where the issues were. As far as I can get working is: Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL]"> <html> <head> <script type="text/javascript" > window.onload=function() { document.getElementById("input1").focus(); } </script> <script type="text/javascript" > function myFunc() { alert("is this working"); } </script> </head> <body> <input id="input1" name="input1" type="text" onkeyup="myFunc()" maxlength="2" value="type a No." /> </body> </html>
First issue with this: When I first get to the page everything is just as ordered. The focus is on the text box and the default value is "type a No." When I press a key onkeyup, I bring up the alert box with "is this working" in it. But when I refresh the page with the refresh button, after clicking the "ok" on the alert box, the character that was typed in is still displayed. If I use the link to the page, or the URL from the address bar, then the page reloads properly with the default value, "type a No".
How do you get the default textbox value using the refresh button as apposed to reloading the page using a link or from the address bar? Using Firefox 8.0 more important at the moment: If I change the script to pass an argument to the function the script crashes. It does not pull up an alert box and freezes. My guess is that I'm doing something wrong on a concept level. Changed the default value of the text box to " ". Looked like to me there was an issue with the size specification and the default string length as you could only enter in a key stroke by highlighting the text and replacing it.
What I wanted to do is to link to a new page having a date range input in the form of 6 text boxes, 2 sets of mm-dd-yy, from and to, where the upon loading the page box 1 of the from would auto focus and then auto tab and then post where php could take over on the server side. Setting up the form and the lay out, no problem. Auto focusing went just like expected using the following code, included because on the next step everything fell apart.
I've been messing with this code for about a couple of hours, and I did everything down to the wire..yet still I am unable to get it to work. When I input the numbers, and click off to the side nothing appears down at the final textarea of the form which is suppose to show the average.I've tried just about everything, sadly all I have to go by is other example codes, and the very intricate instructions which states I must pass the values to the calcAvg() to the second function of performCalc(), which I did, and then I assigned the var calcResult another value. From there I did the return..and after that I'm rather loss as to what to do next to get this code to work, any tips?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
I'm trying to pass an array as an argument for a function on load of an html page, but can't seem to get it to work. Here is my code.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
This is a post relate to this post:DOM inside variable with innerHTML?To reduce any ambiguity this new thread deals with the code below.The previous post was more about how DOM can be used in a variable.This one has to do with a similar problem, but more with functions.
This is the first question I couldn't find answered by Google! No doubt the answer is out there, but...
So I have a function that is being passed an array of nodes: some of them plain vanilla DOM nodes, (say, those created by document.createElement), others are jQuery nodes (say $("<div></div>").
What's the best way to determine if a node is of each type? I can obviously look at the node contents, but I wondered if there was a "best practices" here?
I am writing a Javascript to sort and write out some stuff. PHP is not an option so I need to get this script working.
The problem arises when I want to make a link to the next page. (i.e. Like the link in Google for the next 10 results)
I have <a href=# onclick=FUNCTION(VALUES)> written dynamically by the script.
It is written onto the document correctly, but the problem is, when the link is clicked, some seemingly random values are passed to the function.
When you run the code, the first alert will show all the values that a were passed to the function. The second and third will show what was written to the document.
If you try clicking the next button after the code loads, the values that result will be unusual.
thta should of out put me "hello world" as it is what it is in array but it does not as there is 422 there That's my ID from database, and I gotta use itNow, if I change that ID to something in letters say to "i" then everything is fine. ex
PHP Code:
json_data_object.i.name
That one would work, but then I have to chnge JSON string thta pulls data from DB and i cant really do that...So the question is how can I make 422 (numbers) works the same way as just string values in the array/object tree?
I need to find the average number in javascript. It is the only bit of javascript that I have to do in the course but it carries a high mark of which I cannot afford to lose. Any way the question is : "Write code to calculate the average height and write it out in the browser window"
<HEAD><TITLE> average</TITLE> <SCRIPT LANGUAGE = "JavaScript"> //Experimental results of Table 1 stored in arrays. var Height = [15,16,17,18,19]; var Number = [2,1,6,4,2];
//Part (ii). //Write code to declare and initialise new array to represent the third row of the table. var avg = new Array(5) var avg = ["60","80","187","180","114"] ; avg[0] = "60"; avg[1] = "80"; avg[2] = "187"; avg[3] = "180"; avg[4] = "114"; //Part (iv). //Write code to calculate the average height and write it out in the browser window. avg = 0; for (var count = 1; count <= 5; count = count + 1) Array.average = function(){ var avg = 0; for(var a = 0; a < this.length; a++){ avg += this[a]; } return avg / this.length; };
document.write('average height is ' +avg + '<br>'); </SCRIPT></HEAD><BODY> </BODY></HTML>
I'm trying to pass titleArray and pointsArray to the task(); I'm getting an error mgs this.assignments() is not a function. I've highlighted this.assignments()
how to you use the result of a function as the argument for another function instead of passing the actual function to it. i.e.
function foo2(){}
function foo(func){}
foo(foo2);
This passes foo2 to foo, can I do foo(foo2()) ? i.e. is the difference between whether a function is passed or evaluated dependent on whether you use "()" after the function name?
Im trying to calculate the mean/average of numbers in an array.how can i get the length of an array to perform in a simple / maths problem whilst being flexiable to the length of the array?
She's wanting an array that will store points awarded and then calculate the average. It will also then show the distribution of the allocated grades (that I'm not having trouble with - just pulling the averages from the array data).
This is my code so far:
<!DOCTYPE html PUBLIC "-W3CDTD XHTML 1.0 TransitionalEN" "http:www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http:www.w3.org/1999/xhtml"> <head>
[Code]....
I've tried different ways, most ending in the 'average' variable being undefined or when the button is pressed and the function 'ReturnAverage' called it doesn't do anything. I'm using Eclipse as my IDE to work on this and check syntax.
I am writing a Javascript to sort and write out some stuff. PHP is not an option so I need to get this script working.
The problem arises when I want to make a link to the next page. (i.e. Like the link in google for the next 10 results)
I have <a href=# onclick=FUNCTION(VALUES)> written dynamically by the script.
It is written onto the document correctly, but the problem is, when the link is clicked, some seemingly random values are passed to the function.
I also have an invalid character error that arises when I pass the "whereVARquoted" variable into my function (code 2) which disappears when the variable is removed (code 1).
Can you pass an array reference to a constructor? I don't know if it's because the syntax is screwy or it's impossible, but the following doesn't work.
// row constructor with array called fields function RowObject(fields) { this.fields=new Array(); this.fields=fields; }
I have a list of serial numbers, a from and to set of numbers that correlate to a year. Id like to have a way to have a user enter a serial number, and have the year returned as the answer. For example;
100001 to 100100 = 1990 100101 to 100200 = 1991 100200 to 100300 = 1992
so if the user enters 100145 the answer 1991 would be returned
how to look for the correct code / function its hared to find the answer - catch 22.
On the ONLOAD event I would like a popup box to open. In this popup box I need two text boxes. One for the UserName and one for the BillingTo name. After entering these two items the user can either hit the enter key or press the submit button. The popup window will close and then those two text boxes in the original webpage will be filled in automatically.
I would like to have the two fields to be validated so the popup won't close until both text boxes are filled in.
So far I have used the function from javascript.internet.com in the header of the original page. I have tried to make so that when the first popup closes the second popup will open. I can get the variables to pass the main page but I feel it would be more effecient to have both text boxes in the same popup instead of having two. Code:
It is kind of difficult to explain what I am trying to do here, I will provide the form here to give a better idea.
<ul> <li>Select the type of your starting point of interest:<br/> <div id="start_menu"><form action="test_getrss.php" name="form1" method="post"> <span><input type="radio" value="Apartment" name="start"
[Code]....
I get values for start_time, end_time and semester, but not the last two values. I noticed that the first two forms here are dynamically generated from Ajax.
How can I pass the values of these two selected menus to the third form, to process.php so the values can be retrieved?