sumArray
Вычислить сумму элементов массива, используя reduce
by Anastasia Sharko
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("sumArray", function() {
before(function () {
chai.config.includeStack = false;
});
describe('формат', function(){
it("объявлена", function() {
assert.isFunction(window.sumArray);
});
it("принимает 1 аргумент", function() {
assert.equal(window.sumArray.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.sumArray) == 'function'){
describe('проверки', function(){
var reduceProxy = installProxy(Array.prototype, 'reduce', function pre(){
pre.proxy.called = (pre.proxy.called || 0) + 1;
}, function post(result){
post.proxy.result = result;
});
function sumArrayPlain(array){
var result = 0;
for(var i = 0; i < array.length; ++i)
result += array[i];
return result;
}
function createRandomIntArray(min, max, size){
var result = [];
for(var i = 0; i < size; ++i)
result.push(~~(Math.random() * (max - min)) + min);
return result;
...
JavaScript
function sumArray(array){
}