Javascript - How to check if 3 numbers are consecutive and return starting points?

If I have an array [1, 2, 3, 5, 10, 9, 8, 9, 10, 11, 7] and you need to find each case of three consecutive numbers (regardless of whether it is ascending or descending), like I did it?

The second part would be to warn the array with the index of each of these sequences.

For example, the previous array will return [0,4,6,7] .

So far I have it ... which is a rough start

 var arr = [1, 2, 3, 5, 10, 9, 8, 9, 10, 11, 7]; var results = []; for (var i = 1; i < arr.length; i++) { if ((arr[i] - arr[i-1] != 1) && (arr[i] - arr[i+1] != 1)) { results.push(arr[i]); } } alert(results); 

Thanks for the help!

Thanks for the math.abs pointer. This is what I ended up with:

 var array = [1, 2, 3, 5, 10, 9, 8, 9, 10, 11, 7]; var indexes = []; for(var i=0; i < array.length; i++) { var diff = array[i+1] - array[i]; if(Math.abs(diff)==1 && array[i+1]+diff == array[i+2]) { indexes.push(i); } } alert(indexes); 
+4
source share
5 answers

It would be interesting to know the context of this task ... Anyway, here is my solution:

 var arr = [1, 2, 3, 5, 10, 9, 8, 9, 10, 11, 7]; var results = []; var limit = arr.length - 1; var sequence = 0; for (var i = 0; i < limit; ++i) { var diff = arr[i+1] - arr[i]; if (sequence && sequence === diff) { results.push(i-1); continue; } sequence = (diff === 1 || diff === -1) // or ... Math.abs(diff) === 1 ? diff : 0; } console.log(results); 

The idea is simple: we do not need to compare two neighbors twice. ) This is enough to raise some flag of the sequence if this comparison starts the sequence and drops it if there is no sequence.

+3
source

This is a very literal approach to your question - I checked only the numbers ahead, but adding a reverse would be almost the same way.

 var arr = [1, 2, 3, 4, 10, 9, 8, 9, 10, 11, 7]; var results = []; for (var i = 0; i < arr.length; i++) { // if next element is one more, and one after is two more if (arr[i+1] == arr[i]+1 && arr[i+2] == arr[i]+2){ // store the index of matches results.push(i); // loop through next numbers, to prevent repeating longer sequences while(arr[i]+1 == arr[i+1]) i++; } } console.log(results); 
+3
source

You need to carefully look at your expression in the if statement.

He currently says:

  • If the difference between the current item and the previous item is not 1 and
  • If the difference between the current item and the next item is not 1

then this is the result.

So, at first glance, this is the wrong logical operator to determine if the current element is in the middle of a sequential set of three.

In addition, this does not take into account the ascending or descending set of three.

Try to understand in what language the state will look and from there.

Some things to consider

  • I suggest you start browsing the list of i = 2
  • Study Math.abs
+1
source

This, I think, is an easier way to do this. First, check the average value of the left and right numbers equal to the middle, then check that the absolute value of any of the neighbors is equal to one.

 var arr = [1, 2, 3, 5, 10, 9, 8, 9, 10, 11, 7]; var indexes = []; for(var i=1; i < arr.length; i++) { if((arr[i-1]+arr[i+1]) / 2 == arr[i] && Math.abs(arr[i]-arr[i-1]) == 1) { indexes.push(i-1); } } alert(indexes); 
+1
source
 var arr = [1, 2, 3, 5, 10, 9, 8, 9, 10, 11, 7]; var results = []; for (var i = 0; i < arr.length - 2; i++) { if ((arr[i+1] - arr[i] === 1) && (arr[i+2] - arr[i+1] === 1)) { results.push({ i:i, mode:'up', arr:[arr[i],arr[i+1],arr[i+2] }); } if ((arr[i+1] - arr[i] === -1) && (arr[i+2] - arr[i+1] === -1)) { results.push({ i:i, mode:'down', arr:[arr[i],arr[i+1],arr[i+2] }); } } alert(results); 
0
source

Source: https://habr.com/ru/post/1414241/


All Articles