Hello codecall members
I am very new to javascript as you can probably tell
but i hope that someone can help me finish this off
i need to find the average height via javascript.
ok here is the maths side of it
Total of height 60+80+187+180+114= 621
Total of objects 4+5+11+10+6 = 36
621/36= 17.25
Average height is 17.25
so i have messed about with this script for about a week now and all i have managed to do is get this on screen
The average of the plants 60,80,187,180,114 is 124.2
it isn't even the right answerand i do not need to list the array.string if thats what it is called lol just the total average.
Please please can someone help as my boyfriend said i should give up before i even start
I wish i have something to offer in return but all i have is my broken javascript
thank you to you all
love kelly
Code:<HEAD> <TITLE> averages </TITLE> <SCRIPT LANGUAGE = "JavaScript"> //Experimental results of Table 1 stored in arrays. var height = [15,16,17,18,19]; var number = [4,5,11,10,6]; //Part (ii). //Write code to declare and initialise new array to represent the third row of the table. var myarray = new Array(5) var myarray = ["60","80","187","180","114"] ; //Part (iv). //Write code to calculate the average height and write it out in the browser window. var values = [60, 80, 187, 180, 114]; var total = 0; for ( var i = 0; i < values.length; ++i ) { total += values[i]; } var myarray = total / values.length; document.write("The average height " + values.join(",") + " is " + myarray); </SCRIPT> </HEAD> <BODY> </BODY> </HTML>
When you are calculating the average on the line:
you are dividing the total value of your heights by the amount of values you have which is five. If you want to get the answer that you are looking for, you need to add up the values of the array titled total in a loop and then divide your total by the total of that array. The code to do this might look something like this:Code:var myarray = total / values.length
This should output the number you are looking for. The math was being done correctly all along, but you have to have the right input to get the right outputCode://Insert between the for loop and the reasignment of myarray var averageDivisor = 0; for(var i = 0; i < number.length; i++) averageDivisor += numbers[i]; var myarray = total / averageDivisor; //your output remains the same. Hope this fixes your problem.
hi thank you for your patience
is this what you mean
please ignore my ignorance.
kelly
xxx
Code:<HEAD> <TITLE> averages </TITLE> <SCRIPT LANGUAGE = "JavaScript"> //Experimental results of Table 1 stored in arrays. var height = [15,16,17,18,19]; var number = [4,5,11,10,6]; //Part (ii). //Write code to declare and initialise new array to represent the third row of the table. var myarray = new Array(5) var myarray = ["60","80","187","180","114"] ; //Part (iv). //Write code to calculate the average height and write it out in the browser window. var values = [60, 80, 187, 180, 114]; var total = 0; for ( var i = 0; i < values.length; ++i ) { total += values[i]; } var averageDivisor = 0; for(var i = 0; i < number.length; i++) averageDivisor += numbers[i]; var myarray = total / averageDivisor; var myarray = total / values.length; document.write("The average height " + values.join(",") + " is " + myarray); </SCRIPT> </HEAD> <BODY> </BODY> </HTML>
Yes, except that for the code to work, you should remove the line:
this line is unnecessary and will provide the same incorrect answer you are already receiving. Once you remove that line, your code should give you the output you are looking for.Code:var myarray = total/values.length;
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks