# Array loop problem which returns the difference between the value with fixed value

• May 14th, 2009, 06:08 AM
Array loop problem which returns the difference between the value with fixed value
Good Morning

I hope I can explain a problem that I've tried to code but having problems with.

I have an array of 20 numeric values and the each value is passed (in a loop) along with a fixed value (1.50) to a function that returns the difference between the two.

I have the function coded and working correctly.

for instance

array[0] - 1.50 = 3
array[1] - 1.50 = 2
array[2] - 1.50 = 0.5

etc etc

This has to be done in the loop until the the program finds the first element in the array with a difference of less than 1.

So for instance in the example above if the value of array[2] = 2. then the loop would stop there as it's the first element in the array with a difference < 1.

I can't get the loop structure coded correcty, I trust I need a while loop and a for loop.

Hope you can see the problem and help if possible. Please let me know if you need clarificatiion.

Regards

JS
• May 14th, 2009, 02:50 PM
Fendaril
Re: Array Loop Problem
It would help if I saw your function but ill give it a go:

Code :

```for(i=0;i<array.length;i++) { if(array[i]-1.50< 1) { break; }   }```
• May 14th, 2009, 04:21 PM
JavaPF
Re: Array Loop Problem

Can you post the code you have so far please?
• May 15th, 2009, 03:09 AM
Re: Array Loop Problem
Thanks Fendaril

I think I'll be OK with that. At the break point I would like to assign a value to a variable that equals the element in the array where it breaks.

Such as var breakPoint = point in array where break occured.

Incidentally the user has now requested I code this in Javascript but I can adapt any code you advise.

Thanks Again

JS
• May 15th, 2009, 03:14 AM
Freaky Chris
Re: Array Loop Problem
Code :

```for(i=0;i<array.length;i++){ if(array[i]-1.50< 1){ x = array[i]; break; } }```
Please don't ever get confused between Java and JavaScript.

Chris
• May 15th, 2009, 04:31 AM