PDA

View Full Version : Resolved array

flouriet
10-18-2008, 02:27 PM
resolved

Philip M
10-18-2008, 03:40 PM
Sounds like homework! Have a look at the posting guidlines.

Quizmaster. Who was the Roman god of war? I'll give you a clue. It is the name of a type of chocolate bar.
Contestant: Twix.

flouriet
10-19-2008, 05:58 PM
I get very confused with this stuff, is there any recommended books that I can read to help me. thhanks

Philip M
10-19-2008, 08:02 PM
Your work has quite a lot of errors. I have worked through some of them for you but I have run out of time and don't think I ought to do all your homework for you, so the standard devaition calculation still needs correction. You should also check manually that the various calculations are in fact correct. I trust you will learn from this rather than blindly copy it.

<html>
<script type="text/javascript">
function divide_string() {

var data=prompt("Enter your data (separate with a space)","");
data = data.replace(/[^\d\s]/g,"") // strip non-numeric
data = data.replace(/\s+/g, " "); //strip double spaces

new_array = data.split(" "); // spliting the new array
new_array = new_array.sort(function(a,b){return a - b})
var len= new_array.length; //length of array or number of elements
var halfLen = len/2; // half length of the number of elements
var med = Math.floor(halfLen);// find the median

if(med == halfLen) {
median = (new_array[med+1] - new_array[med])/2;
}
else {
median = new_array[med];
}
var max=new_array[len-1];
var min=new_array[0];

var sum = 0;
var stddevn = 0;
for (i=0; i<len; i++) {
sum = sum + (parseFloat(new_array[i])); // sum
}
var a= "";// each data squared then the sum of it
var b= (sum*sum)/len; // part two of stddevn ( means sum of the data squared divided by number of elements
var mean = (sum/len); // mean
stddevn = Math.sqrt((a-b)/(len-1)); // standard deviation

alert ("Array: "+new_array+", The mean is: " + mean + " The standard deviation is: " + stddevn+ " The median is: " + median + " The maximum is: " + max +" The min is: " + min)
}

</script>
<FORM>
</FORM>

</html>

Here is a script to calculate the standard devaition:-

<script type = "text/javascript">
var data = new Array("73", "58", "67", "93", "33", "18", "147");
var deviation = new Array();
var sum = 0;
var devnsum = 0;
var stddevn = 0;
var len = data.length;
for (var i=0; i<len; i++) {
sum = sum + (data[i] * 1) // ensure number
}
var mean = (sum/len).toFixed(6); // 6 decimal places
for (i=0; i<len; i++) {
deviation[i] = data[i] - mean;
deviation[i] = deviation[i] * deviation[i];
devnsum = devnsum + deviation[i];
}
stddevn = Math.sqrt(devnsum/(len-1)).toFixed(6); // 6 decimal places

alert ("The mean is: " + mean + " The standard deviation is: " + stddevn)
</script>

Sign on a Maine shop: Our motto is to give our customers the lowest possible prices and workmanship.

flouriet
10-19-2008, 08:42 PM
thanks alot
could you tell me why you used a,b and used this function
new_array.sort(function(a,b){return a - b})

thanks for helping me
i realize that some of the equations were wrong but I was able to fix them
thanks once again

Philip M
10-19-2008, 10:08 PM
thanks alot
could you tell me why you used a,b and used this function
new_array.sort(function(a,b){return a - b})

There is a good explanation at
http://www.javascriptkit.com/javatutors/arraysort.shtml

flouriet
10-19-2008, 11:54 PM
somehow this doesn't work

Philip M
10-20-2008, 08:05 AM
Sorry, I can't take over the role of your teacher. :D

var a=[1,2,3,4,5,6];
var len=a.length;
var med=len/2;
if (len&#37;2==0)
median=(a[med]+a[med-1])/2;
else
median=a[Math.floor(med)];

"I have answered three questions, and that is enough,"
Said his father. "Don't give yourself airs!
Do you think I can listen all day to such stuff?
Be off, or I'll kick you down stairs."

- Lewis Carroll

flouriet
10-20-2008, 08:43 AM
i really appreciate your help but when i did the first part i got the same answer as you but when i put it into my code and typed in teh same series of numbers it gave me the median of 22.5

Philip M
10-20-2008, 11:50 AM
Use alerts after each line to check the progress of the calculation. And be careful to ensure that your variables are numbers, not strings.

flouriet
10-20-2008, 12:59 PM
thanks for all of your help
I didn't realize you're from London oh I was there about a year ago and love London.