473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1195
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

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

Similar topics

4
by: Sven Dzepina | last post by:
Hello people =) Has somebody a nice script, which can solve equations ? It would be super, if someone has an idea where I can get such a script / code in php. Thanks. Gretting!
16
by: JustSomeGuy | last post by:
I have a routine that evaluates a polynomial equation that have 3 variables x,y,z of orders 1,2,3 the coefficients of the polynomial are in an array. This routine is quite slow and I'd like to...
2
by: Mike Button | last post by:
Hello all, I am really really desperate on what I should do, and I am asking for help from anyone in this newsgroup, here's the situation: I am creating a form that is being run on a server...
8
by: Bshealey786 | last post by:
Okay im doing my final project for my first computer science class(its my major, so it will be my first of many), but anyway im a beginner so im not to great with C++ yet. Anyway this is the error...
9
by: TG | last post by:
Hi there. Anyone knows how to use numpy / scipy in order to solve this ? * A is an array of shape (n,) * X is a positive float number * B is an array of shape (n,) * O is an array of shape...
8
by: Fabian Braennstroem | last post by:
Hi, I would like to remove certain lines from a log files. I had some sed/awk scripts for this, but now, I want to use python with its re module for this task. Actually, I have two different...
6
by: Trev17 | last post by:
Hello, I am new to C++ and i have tried for several hours to make a program my teacher has given me as a lab. Here is the Lab question: the roots of the quadratic equation ax^2 + bx + c = 0, a...
2
by: DaRok28 | last post by:
// Program Description: // This program solves quadratic equations to find their roots. This // program takes values of a, b, and c as input and outputs the root(s). // The user can repeat the...
2
MrPickle
by: MrPickle | last post by:
I want to take a math equation in a string format then translate this into code. For example; sin(x+2)^4 would translate into: std::pow(4, std::sin(x+2)) but I'm not sure where to start, at...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.