Last active
May 31, 2016 20:18
-
-
Save EdCharbeneau/47fb9ae47d0986d048a5658cbdab2e13 to your computer and use it in GitHub Desktop.
Check for a sequence in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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