Created
May 18, 2015 00:15
-
-
Save tylerdmace/f287c4e5d720fd0a2e2d to your computer and use it in GitHub Desktop.
Test 2: Triple Threat
This file contains 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('assert'); | |
/******************************** | |
* Return true if the array contains, somewhere, three increasing | |
* adjacent numbers like .... 4, 5, 6, ... or 23, 24, 25. | |
* | |
* See the asserts below for examples of input | |
* and expected output. | |
* | |
* If you have node installed, all you need to do to test | |
* your code is run: `node tripleThreat.js`. If you see errors, | |
* it is because the tests below did not pass. Once the | |
* tests do pass, you will see a log of `Success!` | |
* | |
* YOUR CODE BELOW HERE | |
********************************/ | |
function tripleThreat(array) { | |
var i, counter = 0; | |
for(i = 1; i < array.length; i++) { | |
if(array[i] === (array[i-1] + 1)) { | |
counter++; | |
} else { counter = 0; } | |
if(counter === 2) { return true; } | |
} | |
counter = 0; | |
return false; | |
} | |
/******************************** | |
* YOUR CODE ABOVE HERE | |
********************************/ | |
assert.equal( | |
tripleThreat([1, 4, 5, 6, 2]), | |
true | |
); | |
assert.equal( | |
tripleThreat([1, 2, 3]), | |
true | |
); | |
assert.equal( | |
tripleThreat([1, 2, 4, 5, 7, 6, 5, 6, 7, 6]), | |
true | |
); | |
assert.equal( | |
tripleThreat([1, 2, 4, 5, 7, 6, 5, 7, 7, 6]), | |
false | |
); | |
assert.equal( | |
tripleThreat([1,2]), | |
false | |
); | |
assert.equal( | |
tripleThreat([10, 9, 8, -100, -99, -98, 100]), | |
true | |
); | |
assert.equal( | |
tripleThreat([10, 9, 8, -100, -99, 99, 100]), | |
false | |
); | |
console.log('Success!'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment