findExtremeIndex
Найти индекс элемента в массиве, такого что по сравнению со средним арифметическим соседей он или как минимум вдвое меньше (слишком мал), или как минимум вдвое больше (слишком велик). Использовать findIndex
by l_gordienkova
September 11, 2016
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.4.5/mocha.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/3.5.0/chai.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.4.5/mocha.min.css">
<div id="mocha">
</div>
<script>
mocha.setup('bdd');
var assert = chai.assert;
var expect = chai.expect;
</script>
<script>
describe("findExtremeIndex", function() {
before(function () {
chai.config.includeStack = false;
});
describe('формат', function(){
it("объявлена", function() {
assert.isFunction(window.findExtremeIndex);
});
it("принимает 1 аргумент", function() {
assert.equal(window.findExtremeIndex.length, 1);
});
});
function installProxy(target, method, pre, post){
var proxy = target[method] = function(delegate){
return function(){
if(pre) pre.apply(this, arguments);
var result = delegate.apply(this, arguments);
if(post) post.apply(this, [result].concat(Array.apply(0, arguments)));
return result;
}
}(target[method]);
if(pre) {
proxy.pre = pre;
pre.proxy = proxy;
}
if(post){
proxy.post = post;
post.proxy = proxy;
}
return proxy;
}
if(typeof(window.findExtremeIndex) == 'function'){
describe('проверки', function(){
var proxy = installProxy(Array.prototype, 'findIndex', function pre(){
pre.proxy.called = (pre.proxy.called || 0) + 1;
}, function post(result){
post.proxy.result = result;
});
function rnd(min, max){
return ~~(Math.random() * (max - min)) + min;
}
function createRandomIntArray(min, max, size){
var result = [];
for(var i = 0; i < size; ++i)
result.push(rnd(min, max));
return result;
}
function suite(name, size, value){
...
JavaScript
function findExtremeIndex(array){
return array.findIndex(function(elem,i,arr){
if(((arr[i-1]+arr[i+1]) < elem) || ((arr[i-1]+arr[i+1])/4 > elem))
return i;
})
}