469,326 Members | 1,590 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,326 developers. It's quick & easy.

need help with an equation in an array

4
HI everyone

i can't figure out what's wrong with this one code

Expand|Select|Wrap|Line Numbers
  1. var a=[1,2,3,4,5,6];
  2. var len=a.length;
  3. var med=len/2;
  4. if (len%2==0)
  5. median=(a[med]+a[med-1])/2;
  6. else
  7. median=a[Math.floor(med)];
  8. alert(median); 
and the right answer is 3.5 but why is it when i put it into my code and enter the same number it ends up being 22.5

here my code
Expand|Select|Wrap|Line Numbers
  1. var med= len/2; // length divided by 2 
  2. if (len%2==0)
  3. median=(new_array[med]+ new_array[med-1])/2;
  4. else
  5. median=new_array[Math.floor(med)];
can someone help me? I can pm the whole code if needed thanks
Oct 20 '08 #1
5 1047
gits
5,390 Expert Mod 4TB
where did you define new_array? ... when i just replace the code and use new_array as a then it works identical ...

kind regards
Oct 20 '08 #2
pn46
4
where did you define new_array? ... when i just replace the code and use new_array as a then it works identical ...

kind regards
thanks alot you're from munich oh i visited there before and absolutely love it.
Oct 20 '08 #3
gits
5,390 Expert Mod 4TB
i assume you should parseFloat() or parseInt() the values in the array or at least when you want to calculate with them ... because they are of type string when you use it from any webinput ...

;) ... yes munich is quite a nice place to live and work ... especially when you like the near mountains. i'm looking forward to the winter and its snow - reminds me to get the board out of its 'summer-sleep' ;)
Oct 20 '08 #4
pn46
4
i tried to parseFloat it
Expand|Select|Wrap|Line Numbers
  1. if (len%2==0)
  2.   median= parseFloat((new_array[med]+ new_array[med-1])/2);
  3. else
  4.   median=new_array[Math.floor(med)];
but it still doesnt work

Expand|Select|Wrap|Line Numbers
  1. var data=prompt("Enter your data (separate with a comma)","");
  2. new_array = data.split(","); 
then i sorted it
but yeah it's beautiful there i actually went to salzburg and i love it there. I would like to go back to visit I really enjoy my time in Europe. I'm actually considering moving there since the people are so laid back rather than here in the us.
Oct 20 '08 #5
gits
5,390 Expert Mod 4TB
the line:

Expand|Select|Wrap|Line Numbers
  1. median= parseFloat((new_array[med]+ new_array[med-1])/2);
  2.  
should be:

Expand|Select|Wrap|Line Numbers
  1. median= (parseFloat(new_array[med]) + parseFloat(new_array[med-1]))/2;
kind regards

PS: you certainly cannot find us germans 'laid back' ;) or do you? i agree with the other europeans but not the germans ... or it is just a kind of thing that everybody don't like what he has every day (at least for me) ... so everything that is new or unknown seems to be better then our dayly environment :)
Oct 20 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Sven Dzepina | last post: by
16 posts views Thread by JustSomeGuy | last post: by
2 posts views Thread by Mike Button | last post: by
9 posts views Thread by TG | last post: by
8 posts views Thread by Fabian Braennstroem | last post: by
6 posts views Thread by Trev17 | last post: by
MrPickle
2 posts views Thread by MrPickle | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.