Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript check if consecutive array

 /*
  Given an array of number, group algebraic sequences with d=1
  [1,2,3,4,5,6] => true
  [1,2,4,5,6] => false
 */
 var arr = [1,2,4,5,6];
 const differenceAry = arr.slice(1).map(function(n, i) { return n - arr[i]; })
 const isDifference= differenceAry.every(value => value == 1)
 console.log(isDifference);			// False

/*
console.log(arr.slice(1))
console.log(arr.slice(1).map((n, i) => n - arr[i])) // arr.value - arr.index: 2-1=1, 3-2=1, 5-3=2 << false if !1
console.log(arr.slice(1).map((n, i) => n - arr[i]).every(value => value === 1))
*/
 
PREVIOUS NEXT
Tagged: #javascript #check #consecutive #array
ADD COMMENT
Topic
Name
5+3 =