need help with an equation in an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pn46
    New Member
    • Oct 2008
    • 4

    #1

    need help with an equation in an array

    HI everyone

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

    [CODE=javascript]var a=[1,2,3,4,5,6];
    var len=a.length;
    var med=len/2;
    if (len%2==0)
    median=(a[med]+a[med-1])/2;
    else
    median=a[Math.floor(med)];
    alert(median); [/CODE]
    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
    [CODE=javascript]var med= len/2; // length divided by 2
    if (len%2==0)
    median=(new_arr ay[med]+ new_array[med-1])/2;
    else
    median=new_arra y[Math.floor(med)];[/CODE]

    can someone help me? I can pm the whole code if needed thanks
    Last edited by gits; Oct 20 '08, 08:14 AM. Reason: added code tags
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    where did you define new_array? ... when i just replace the code and use new_array as a then it works identical ...

    kind regards

    Comment

    • pn46
      New Member
      • Oct 2008
      • 4

      #3
      Originally posted by gits
      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.

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        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' ;)

        Comment

        • pn46
          New Member
          • Oct 2008
          • 4

          #5
          i tried to parseFloat it
          [CODE=javascript]if (len%2==0)
          median= parseFloat((new _array[med]+ new_array[med-1])/2);
          else
          median=new_arra y[Math.floor(med)];[/CODE]
          but it still doesnt work

          [CODE=javascript]var data=prompt("En ter your data (separate with a comma)","");
          new_array = data.split(",") ; [/CODE]
          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.
          Last edited by gits; Oct 20 '08, 12:26 PM. Reason: added code tags

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            the line:

            [CODE=javascript]median= parseFloat((new _array[med]+ new_array[med-1])/2);
            [/CODE]
            should be:

            [CODE=javascript]median= (parseFloat(new _array[med]) + parseFloat(new_ array[med-1]))/2;[/CODE]
            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 :)

            Comment

            Working...