Skip to content

Instantly share code, notes, and snippets.

@AndSheCodes2
Created March 11, 2019 10:14
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 AndSheCodes2/4e4729713b6ab7163aab0f0a8a6a715a to your computer and use it in GitHub Desktop.
Save AndSheCodes2/4e4729713b6ab7163aab0f0a8a6a715a to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/henixov
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
/*
Given an array of unsorted positive integers,
write a function that finds runs of 3 consecutive
numbers (ascending or descending) and returns the
indices where such runs begin. If no such runs are
found, return null.
function findConsecutiveRuns(input:Array):Array
Example: [1, 2, 3, 5, 10, 9, 8, 9, 10, 11, 7]
would return [0, 4, 6, 7]
Please build a unit test suite to demonstrate that the
function meets the stated requirements (Mocha is preferred).
*/
"use strict";
var arr = [1, 2, 3, 5, 10, 9, 8, 9, 10, 11, 7];
var findConsecutiveRuns = function findConsecutiveRuns(arr) {
var diff1,
diff2,
result = [];
for (var i = 0, len = arr.length; i < len - 2; i += 1) {
diff1 = arr[i] - arr[i + 1];
console.log("diff1 " + diff1);
diff2 = arr[i + 1] - arr[i + 2];
console.log("diff2 " + diff2);
if (Math.abs(diff1) === 1 && diff1 === diff2) {
result.push(i);
console.log("Results " + result);
}
}
return result.length > 0 ? result : null;
};
findConsecutiveRuns(arr);
/*
const assert = require('assert');
const Mocha = require('mocha');
const mocha = new Mocha();
mocha.suite.emit('pre-require', this, 'solution', mocha);
describe('Method: findConsecutiveRuns', () => {
it('should return null if there is no run', () => {
assert.deepEqual(findConsecutiveRuns([1, 2, 4, 6, 7]), null);
});
it('should return correct indexes of runs', () => {
assert.deepEqual(findConsecutiveRuns([1, 2, 3, 5, 10, 9, 8, 9, 10, 11, 7]), [0, 4, 6, 7]);
});
});
mocha.run(); */
</script>
<script id="jsbin-source-javascript" type="text/javascript">/*
Given an array of unsorted positive integers,
write a function that finds runs of 3 consecutive
numbers (ascending or descending) and returns the
indices where such runs begin. If no such runs are
found, return null.
function findConsecutiveRuns(input:Array):Array
Example: [1, 2, 3, 5, 10, 9, 8, 9, 10, 11, 7]
would return [0, 4, 6, 7]
Please build a unit test suite to demonstrate that the
function meets the stated requirements (Mocha is preferred).
*/
var arr = [ 1, 2, 3, 5, 10, 9, 8, 9, 10, 11, 7 ];
var findConsecutiveRuns = function(arr){
var diff1, diff2, result = [];
for ( var i = 0, len = arr.length; i < len - 2; i += 1 ) {
diff1 = arr[i] - arr[i+1];
console.log("diff1 "+ diff1);
diff2 = arr[i+1] - arr[i+2];
console.log("diff2 "+ diff2);
if ( Math.abs( diff1 ) === 1 && diff1 === diff2 ) {
result.push( i );
console.log("Results "+ result);
}
}
return result.length > 0 ? result : null;
}
findConsecutiveRuns(arr);
/*
const assert = require('assert');
const Mocha = require('mocha');
const mocha = new Mocha();
mocha.suite.emit('pre-require', this, 'solution', mocha);
describe('Method: findConsecutiveRuns', () => {
it('should return null if there is no run', () => {
assert.deepEqual(findConsecutiveRuns([1, 2, 4, 6, 7]), null);
});
it('should return correct indexes of runs', () => {
assert.deepEqual(findConsecutiveRuns([1, 2, 3, 5, 10, 9, 8, 9, 10, 11, 7]), [0, 4, 6, 7]);
});
});
mocha.run(); */</script></body>
</html>
/*
Given an array of unsorted positive integers,
write a function that finds runs of 3 consecutive
numbers (ascending or descending) and returns the
indices where such runs begin. If no such runs are
found, return null.
function findConsecutiveRuns(input:Array):Array
Example: [1, 2, 3, 5, 10, 9, 8, 9, 10, 11, 7]
would return [0, 4, 6, 7]
Please build a unit test suite to demonstrate that the
function meets the stated requirements (Mocha is preferred).
*/
"use strict";
var arr = [1, 2, 3, 5, 10, 9, 8, 9, 10, 11, 7];
var findConsecutiveRuns = function findConsecutiveRuns(arr) {
var diff1,
diff2,
result = [];
for (var i = 0, len = arr.length; i < len - 2; i += 1) {
diff1 = arr[i] - arr[i + 1];
console.log("diff1 " + diff1);
diff2 = arr[i + 1] - arr[i + 2];
console.log("diff2 " + diff2);
if (Math.abs(diff1) === 1 && diff1 === diff2) {
result.push(i);
console.log("Results " + result);
}
}
return result.length > 0 ? result : null;
};
findConsecutiveRuns(arr);
/*
const assert = require('assert');
const Mocha = require('mocha');
const mocha = new Mocha();
mocha.suite.emit('pre-require', this, 'solution', mocha);
describe('Method: findConsecutiveRuns', () => {
it('should return null if there is no run', () => {
assert.deepEqual(findConsecutiveRuns([1, 2, 4, 6, 7]), null);
});
it('should return correct indexes of runs', () => {
assert.deepEqual(findConsecutiveRuns([1, 2, 3, 5, 10, 9, 8, 9, 10, 11, 7]), [0, 4, 6, 7]);
});
});
mocha.run(); */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment