Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

This is a collection of different algorithms, written in JavaScript.
The purpose of this package is to define basic algorithms in a concise,
but readable form. However, no (pre)mature optimizations should be expected
but readable form. However, no (pre)mature optimizations should be expected
here and code should never be used in production.

## Contents

### Iterative

- [x] coprime
- [x] maxsubarray
- [x] prime

### Recursive
Expand Down
18 changes: 18 additions & 0 deletions lib/iterative/maxsubarray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

/**
* Expose `maxsubarray`.
*/

module.exports = maxsubarray;

function maxsubarray(array) {
var maximum = 0;
var current = 0;

for (var i = 0; i < array.length; i++) {
current = Math.max(0, current + array[i]);
maximum = Math.max(maximum, current);
}

return maximum;
}
7 changes: 7 additions & 0 deletions lib/iterative/test/maxsubarray-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var test = require('tape');
var maxsubarray = require('../maxsubarray');

test('prime(number)', function (t) {
t.equal(maxsubarray([-2, 1, -3, 4, -1, 2, 1, -5, 4]), 6, 'should be equal 6');
t.end();
});