Skip to content

Commit

Permalink
refactor: rewrite subtask recursive-depth
Browse files Browse the repository at this point in the history
  • Loading branch information
AlreadyBored committed Feb 9, 2020
1 parent 04176c3 commit 962299e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,15 @@ Write your code in `src/simple-chain.js`.
### **Recursive depth calculator**
![Go deeper](https://i.gifer.com/S5u.gif)

Your task is to implement function `calculateDepth` that takes an `array` and returns its depth.
Your task is to implement class `DepthCalculator` with method `calculateDepth` that takes an `array` and returns its depth.

`calculateDepth` function must pass the given array **recursively**. Depth of a **flat** array is 1. Function must correctly work with `arrays` that contain no elements or conatin empty `arrays`.
`calculateDepth` method must pass the given array **recursively**. Depth of a **flat** array is 1. Method must correctly work with `arrays` that contain no elements or conatin empty `arrays`.

For example:

`const depthCalc = new DepthCalculator();`
`const { calculateDepth } = depthCalc;`

`calculateDepth([1, 2, 3, 4, 5])` => `1`

`calculateDepth([1, 2, 3, [4, 5]])` => `2`
Expand Down
6 changes: 3 additions & 3 deletions src/recursive-depth.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = class MyClass {
module.exports = class DepthCalculator {
calculateDepth(arr) {
// magic
// write your code here
}
}
};
10 changes: 7 additions & 3 deletions test/recursive-depth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const sinon = require('sinon');

Object.freeze(assert);

const MyClass = require('../src/recursive-depth.js');
const instance = new MyClass();
const DepthCalculator = require('../src/recursive-depth.js');
const instance = new DepthCalculator();
const calculateDepth = instance.calculateDepth.bind(instance);

const createFlatArr = (length) => Array.from({length}, () => Math.floor(Math.random() * length));
Expand Down Expand Up @@ -36,11 +36,15 @@ describe('Recursive depth', () => {
assert.equal(calculateDepth([1, [8, [[]]], 2, 3, [8, [[[[[[[[[[[[[]]]]]]]]]]]]]], [8, [[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]], 4, 5, ['6575',['adas', ['dfg', [0]]]]]), 25);
assert.equal(calculateDepth([1, [8, [[]]], [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]], []]]], []]]]]]]]], []]]], []]]]]]]]]], 2, 3, [8, [[[[[[[[[[[[[[]]]]]]]]]]]]]]], [8, [[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]], 4, 5, ['6575',['adas', ['dfg', [0]]]]]), 31);
});
it('works recursively?', () => {
it('works recursively', () => {
const spy1 = sinon.spy(instance, 'calculateDepth');
assert.equal(calculateDepth([1, 2, 3, 4, 5, [1, []]]), 3);
expect(spy1.callCount).to.be.greaterThan(1);
spy1.restore();
const spy2 = sinon.spy(instance, 'calculateDepth');
assert.equal(calculateDepth([[[[[]]]]]), 5);
expect(spy2.callCount).to.be.greaterThan(1);
spy2.restore();
});
});
});

0 comments on commit 962299e

Please sign in to comment.