I've now got to form an average of snowfall inputs, taken from looped prompts, however I'm not allowed to use arrays or functions...Almost every example I see uses arrays, such as this one here:http://www.codingforums.com/showthread.php?t=4313Is it possible to not use arrays to form the average? Please describe how to do this in general terms, as was highlighted in that link ^^^ I want to learn, not copy, although one can be derived from the other...What I haveso far, assume all vars have been announced.
for (var d=1; d<=numofinputs; d=d+1)
{
input = prompt("Enter a data input" + d)
}
I am having trouble with a project i am supposed to be doing which is to turn structured English into coding joined with the code i am about to post !This is the code i have written so far that works:
var contestantNamesArray = ['Tom and Nazia', 'Pat and Dan', 'Sandra and Kofi', 'Ian and Adele', 'Paul and Costas']; var judgesPointsArray = [2,1,5,4,3];[code].....
I have tried myself but i am stuck at how to link the contestantNamesArray with the rest of the code in order to be able to display the couples who scored the maximum points and store it in a new variable and then write out the names.
I've been working on trying to compile this bit of Java code. It's taken from an existing code (with permission) that I know works. The problems started when I added an array. Code:
Write a script to generate two random numbers for variable m and n, the values generated for the variables should range from 1 to 10. We want to make an m * n table (m rows and n columns) with the word Hello in each of its cells. Now define a function f with one parameter n that displays n columns of one row of the table (You need a for-loop). Call this function m times to display m rows of the table. For example if m = 6 and n = 4 we should get the following:
I am confused about the true difference between the two below examples.
first example:
// Demonstrating a problem with closures and loops var myArray = [“Apple”, “Car”, “Tree”, “Castle”]; var closureArray = new Array();
[code]....
Here we iterate through the length of myArray, assigning the current index of myArray to theItem variable. We declare closureArray 4 times as an anonymous function. The anonymous function in turn declares the predefined write() function, which is passed parameters. Since write() is in closureArray() a closure is created??? During each iteration, theItem is reassigned its value. The four closures reference this value. Since they reference this same value and since this value is reassigned ultimately to the value of the fourth index position, tHe time we execute closureArray later on, all four closures output the same string. This is because all four closures are within the same scope "the same environment" and therefore are referencing the same local variable, which has changed.
I have a couple of problems with this example:
1) I thought a closure is a function that is returned - the inner function is not returned above.
2) theItem is not even a local variable of the parent function (closureArray) - I thought in order for a closure to work, the inner function only accesses the local variables of the outer function, but in this case the local variable is defined OUTSIDE of the parent function.
3) the "the four closures are sharing the same environment." The thing is even in the second example, they are sharing the same environment.
Second example:
// A correct use of closures within loops var myArray = [“Apple”, “Car”, “Tree”, “Castle”]; var closureArray = new Array();
[code]....
Here we iterate over the length of myArray (4 times), assigning the index of myArray to theItem variable. We also return a function reference to the closureArray during each iteration (closureArray[i]), where i is index number so we assign 4 functon references. So when we iterate through myArray, we immediatelly call the writeItem() fucntion passing an argument of theItem at its current value. This returns a child anonymous function and when that child function is called, it will execute a block that calls the predefined write() method. We assign that returned anonymous function to the variable closureArray. Hence, closureArray holds a reference to that anonymous function. So closureArray during each iteration holds a reference to the anonymous function and we later call closureArray, which in turn calls the anonymous function, therefore calling the predefined write() function to output the local variable of the parent function. This outputs each distinct index of myArray.
This is because since we created the closure, when we call writeItem, passing theItem argument, since theItem is a local variable of the parent function of the closure, it is never destroyed when we later call closureArray (the reference to the child anonymous function)? Yet weren't we using a closure in the first example as well? So whey wasn't those variables preserved?
I don't think it has anything to do with assigning a returned anonymous function to closureArray. Even though an anonymous function creates a new memory position in the javascript engine, therefore not overwriting the other function references we create during the iteration, it's still referring to a local variable declared outside the reference. So if it's about the closure retaining value of parent's local variable even after exiting the parent function allowing for the current indexes to be preserved, then why did the closure in the first example fail to retain each index?
looking for a way to pass an array to a function. ==================================== <script> function myfunction(arrayname) { document.write("blablabla"+ arrayname[1] +"blablabla"); } </script>
<html stuff>
<script> arrayname = new Array("what", "is", "wrong"); myfunction(arrayname); </script> ====================================
I am working on a simple Expanding Family Tree. There are two principal Objects: Person and Family, and two subsidiary ones: Sibling-sets and multi-Marriage-sets.
When I click on a Family Button, I want to display all the Persons of a Sibling-set. Married Siblings will have a Button. Pressing this should display one or more Marriage-Partners. If these Marriages (Families) have children, they will have a Family-Button.
You will notice the code for the four objects is fairly repetitious, but that for the variable-length Sibling and Marriage Arrays, is what has kept me struggling for days. Lines S04-S10 and M04-M10 appear to be the problems.
Even though I think addSibling() is constructing Sibling objects, S06 reads the Argument vector as a single item S07 tells me the argument-count is 1. so S09 processes only once, and then.. it interprets argv[s] as a single item, but assigns the whole array to this.sibA.
function doSomthing(arrayset1, arrayset2, nutherVariable){ document.theForm.txtBox.value=arrayset1[n] + arrayset2[n]; <!-- No output to form textfield unless array defined within function--> } </Script> </Head> <Body> Passes and recieves variables to/from function(s) via Form </Body
I think what I am asking is what is the syntax to do this.
I am attempting a simple average calculation with javascript. I would like to have 6 boxes, 5 for the user to input a number in each, and the 6th box to display the average of the five. I have set up 2 functions to take the input, calc the total, then calculate the average. It seems to be taking the input, but on the rare occasions I get anything in the results box, it is NaN.
Here is my code: <!DOCTYPE html PUBLIC "-W3CDTD XHTML 1.0 TransitionalEN" "[URL]"> <html xmlns="[URL]"><head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title>Calculate Average</title> <link href="style.css" rel="stylesheet" type="text/css" /> <script language="javascript" type="text/javascript"> /* <[CDATA[ */ Declare variables var calcResult Result of numbers added together and deivided by 5.
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?
I'm a newbie to javascript and I made this script <script> var num = parseInt(prompt("Enter the number of integers to follow")); var sum = 0; for (i = 0; i < num; i++){ sum += parseInt(prompt("Enter a number")); }
if (isNaN(num)) { alert("Invalid"); } else { if (sum < 0) { document.writeln("The sum is 0 and the average is 0"); } else { document.writeln("The sum is " + sum + " and the average is " + sum/num); } } </script>
The scenario : Create in javascript that will read a series of integers at the terminal. The first integer is special, as it indicates how many more integers will follow. Your javascript is to calculate the sum and average of the integers, excluding the first integer, and display these values to the screen. If the total is not greater than 0 then display "The sum is 0 and the average is 0". Did I write the script correctly? am I missing anything that a dumb person might do? for example the person might type in letters instead of numbers.
i am trying to create a interface to average down variable amount of input and show them on screen as they type....this is what i came up with.....and the input box fields....these are dynamically created...
My knowledge of programming is very limited. I would like to calculate the average of session variables in Interpreted Java. However, some of session variables may not always be available. The first thing I do is remove the non numerical portion of the session variables:
String [] variables = {"A",'B','C','D','E'}; i = 0; Iterate through each variable in the array above while (i < variables.length){ Get the variables to be fixed value = session.getVariable(variables[i]); Log the UNFIXED values session.log("UNFIXED: " + variables[i] + " = " + value); if(value != null){ Remove non-numerical elements value = value.replaceAll("\\D", ""); Set variables with new values dataRecord.put(variables[i], value); session.setVariable(variables[i], value); Log the FIXED values session.log("FIXED " + variables[i] + " = " + session.getVariable(variables[i])); } i++; }
Then I make sure the session variables can be treated as numbers: A = Integer.parseInt(session.getVariable("A")).intValue(); B = Integer.parseInt(session.getVariable("B")).intValue(); C = Integer.parseInt(session.getVariable("C")).intValue(); D = Integer.parseInt(session.getVariable("D")).intValue(); E = Integer.parseInt(session.getVariable("E")).intValue(); Are they still alive? session.log("A**" + A + "**"); session.log("B**" + B + "**"); session.log("C**" + C + "**"); session.log("D**" + D + "**"); session.log("E**" + E + "**");
I would like to calculate the average of A and B and save it as session variable AB, calculate the average of C and D and save it as session variable CD, and then the average of AB, CD, and E. Here's the weird part; AB, CD, and or E will not always be available. For example, AB will not be available so I need to calculate the average of CD and E or E will not be available so I need to calculate the average of AB and CD. As I said, my knowledge of programming is very limited so if you could provide code snippet with explanation of each step and where to plug in my session variables.
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.
My task was to build a simple GPA calculator using select input and Javascript to average it all together and return it to the user with his name. So I started with my form which looks like this
Now to the best of my knowledge that looks correct, I made sure to use integers as the values so I could average them using this function <script type="text/javascript"> <!-- function Average(v1,v2,v3){ return Average (v1+v2+v3) / 3; } --> </script>
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>
ich got a textarea and want to add a special symbol to it like average. would be awesome if there were a button next to the textfield where a dialog pops up. via click on the specific symbol it should be added at the promt in the textarea box. maybe something like that smiley input in this wysiwyg-editor. should be as easy for the user as possible.does anybody know a plugin for that problem? i googled a lot and didn't find anything suitabl
I've written a program which will prompt for a number of cities, Prompt for the name of the city, then prompt for the number of snowfall readings of that city, and then prompt for each of these individual snowfall readings of that city. From this, it adds up each of the individual snowfall readings of that city, and will calculate an average by dividing this figure [the total snowfall] by the total number of readings for that city. This average is used to then classify the city as "not snowy", "mild", or "blizzard". I'm happy to PM my code to anyone willing to help out, as I realise this is a complex structure to visualise perhaps, but I can't post it publicly.
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">
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...
It could be a two function javascript or two separate javascripts, I am not that particular as long as it does what I need it to do. The way I would like to see it work is you type the number you have into a field, and press a button to convert it. If it was a two function javascript you would have to select the conversion type you want do first.
i am trying to do but I have no clue how to write it. Im trying to write a while loop that prints out all of the multiples of 5 between 10 and 95. I just need to know how to write the while loop i got everything else.