Skip to content

Instantly share code, notes, and snippets.

@jshawl
Created December 11, 2013 01:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jshawl/7903652 to your computer and use it in GitHub Desktop.
Save jshawl/7903652 to your computer and use it in GitHub Desktop.
var array = [1,2,3,5,10,9,8,9,10,11,7];
function findConsecutiveRuns(array){
var consecutiveRuns = new Array();
for (index=0;index<array.length;index++){
var one = parseInt(array[index]);
var two = parseInt(array[index+1]);
var three = parseInt(array[index+2]);
if (one+1==two && two + 1 == three){
// consecutive increasing: 1,2,3
consecutiveRuns.push(index);
}
if (one-1==two && two-1==three){
// consecutive decreasing: 3,2,1
consecutiveRuns.push(index);
}
}
if(consecutiveRuns.length){
return consecutiveRuns;
} else {
return null;
}
}
findConsecutiveRuns(array);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment