Skip to content

Instantly share code, notes, and snippets.

@EdCharbeneau
Last active May 31, 2016 20:18
Show Gist options
  • Select an option

  • Save EdCharbeneau/47fb9ae47d0986d048a5658cbdab2e13 to your computer and use it in GitHub Desktop.

Select an option

Save EdCharbeneau/47fb9ae47d0986d048a5658cbdab2e13 to your computer and use it in GitHub Desktop.
Check for a sequence in JavaScript
var assert = require('chai').should();
function* mapProgressive(a, fn) {
for (let index = 0; index < a.length - 1; ++index) {
yield fn(a[index], a[index + 1]);
}
}
function isArrayInSequence(a) {
return Array.from(mapProgressive(a, (x,y) => {return x + 1 == y})).every(n => n == true)
}
describe('Array', function() {
describe('isInSequence', function () {
it('should return true when the array is in numerical sequence', function () {
var a = [1,2,3];
isArrayInSequence(a).should.be.true;
});
it('should return false when the array is not numerical sequence', function () {
var a = [8,2,4];
isArrayInSequence(a).should.be.false;
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment