Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: array

1. ## array

resolved

• Sounds like homework! Have a look at the posting guidlines.

Show us your effort, and then perhaps someone will help you improve it.

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.

• I get very confused with this stuff, is there any recommended books that I can read to help me. thhanks

• 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.

Code:
<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>
<INPUT TYPE="button" onClick="divide_string()" value="Click here to enter your data">
</FORM>

</html>
Here is a script to calculate the standard devaition:-

Code:
<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.

• ## Users who have thanked Philip M for this post:

forthfriend (10-20-2008)

• 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

• Originally Posted by flouriet
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

• somehow this doesn't work

• Sorry, I can't take over the role of your teacher.

Code:
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)];
alert(median);  // RESULT 3.5
"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

• 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

• Use alerts after each line to check the progress of the calculation. And be careful to ensure that your variables are numbers, not strings.

• ## Users who have thanked Philip M for this post:

flouriet (10-20-2008)

• 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.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•