1

I've looked around and have not found any answers for this. The problem seems pretty simple at first.

Let's say we have an array like this. We can assume that the array will ALWAYS have some equal thing it counts up by (each element has the same difference between the elements adjacent to it).

[1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.0]

The answer to above would be 0.1.

Another example might be an array like this.

[0.2,0.4,0.6,0.8,1.0,1.2,1.4]

This answer would be 0.2

One possible solution is I could read the first and second parameters of the array and capture the difference and that will be my 'step'. Is this the best way to go about doing this? Is there any downsides to this approach?

if the array was counting downward like this:

[0.8,0.6,0.4,0.2], then how could I extract the step successfully?

In this case I need the answer -0.2, my answer would not work, and taking the absolute value would give me 0.2 not -0.2.

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
Joseph Astrahan
  • 8,659
  • 12
  • 83
  • 154
  • 1
    You don't need any special-case logic for the case of counting downward: In your second example, you simply take `0.6-0.8`, and get the correct answer: `-0.2`. This is similar to how in your first example, you take `0.4-0.2` and also get the correct answer. – Cam Jun 11 '22 at 01:49
  • but then 1.0 - 1.1 will give -0.1 when the answer should be 0.1. I think I need some logic to determine if the first number is larger than the second and then determine negative sign? – Joseph Astrahan Jun 11 '22 at 01:50
  • You should subract like this: `array[1] - array[0]`. So in your first example, that's `1.1 - 1` which yields the right answer. – Cam Jun 11 '22 at 01:53

4 Answers4

1

You don't need any special-case logic for the case of counting downward. In your second example, you simply subtract the first term from the second, 0.6 - 0.8, and get the correct answer: -0.2. This is similar to how in your first example, you subtracted the first term from the second, 0.4 - 0.2 and got the correct answer.

To convince you that this works, consider that your array starts with a real number n, and increases by a real number k each step:

[n, n + k, n + k*2, n + k*3, ...]

Then you can subtract the first two terms:

(n + k) - n

to get

k

The sign of k doesn't matter.

Cam
  • 14,930
  • 16
  • 77
  • 128
1

This is the same as taking the common difference of an arithmetic progression in mathematics. You can just subtract a term from the previous term, and you'll get the difference. Here is how the array would look in general terms:

[a, a + d, a + 2d, a + 3d, ...]

So from this you can easily see that if you perform (a + d) - (a), you'll get d. Subtracting any element from the previous element will yield the same result. So the simplest way to do this would be:

const getStep = array => (array[1] - array[0]);

Demonstration using your three provided arrays:

const getStep = array => (array[1] - array[0]);

console.log(getStep([1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.0]));
console.log(getStep([0.2,0.4,0.6,0.8,1.0,1.2,1.4]));
console.log(getStep([0.8,0.6,0.4,0.2]));

Note that the above values may be inaccurate as they're floating point numbers (Floating point math is broken) but you can just round it to the nearest x-th decimal place (here's a solution with a 1/10th rounding, for instance):

const getStep = array => Math.round((array[1] - array[0]) * 10) / 10;

console.log(getStep([1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.0]));
console.log(getStep([0.2,0.4,0.6,0.8,1.0,1.2,1.4]));
console.log(getStep([0.8,0.6,0.4,0.2]));
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • that is what I was looking for, you are right, this handles even negative numbers – Joseph Astrahan Jun 11 '22 at 01:53
  • 1
    Yep! Super simple - here's some more info if you need it :) https://www.geeksforgeeks.org/arithmetic-progression-common-difference-and-nth-term-class-10-maths/#:~:text=In%20other%20words%2C%20we%20can,%E2%80%93%20an%2D1).&text=If%20the%20common%20difference%20is%20negative%20then%20AP%20decreases. – Jack Bashford Jun 11 '22 at 01:53
  • Yeah the rounding fix really helps, I was just about to ask about that too – Joseph Astrahan Jun 11 '22 at 01:57
  • 1
    No worries :) you can change the values in the rounding really easily as well, just change `10` to whatever xth you need (i.e. for one thousandth precision, just use `1000` instead of `10`) – Jack Bashford Jun 11 '22 at 01:58
  • Yeah, it's a very clean nice answer, great job. I'm impressed, really! Got it all down to one line of code – Joseph Astrahan Jun 11 '22 at 02:00
  • @JosephAstrahan :) my pleasure to help, and yes it's super concise I agree. Simple high-school maths comes back every day :D – Jack Bashford Jun 11 '22 at 02:32
0

This in math is called arithmetic progression. If you guarantee that the sequence have the same step, you surely can just take the difference between the 2 first numbers of the sequence.

when you count downwards your sequence is decrescent, so, your step is negative. There is no possible way to get this sequence downwards without a negative step

Cesar Lopes
  • 373
  • 1
  • 10
0

This function:

function arrayStep(arrayValue) {
  let step = arrayValue[1] - arrayValue[0];
  return step;
}

ps: if I understand correctly, the answer of the first example would be 0.1

rmgimenez
  • 134
  • 4