Skip to content
Open
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
10 changes: 10 additions & 0 deletions src/splitInteger.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
* @returns {number[]}
*/
function splitInteger(value, numberOfParts) {
if (value < numberOfParts) {
const output = [];

for (let i = 0; i < numberOfParts; i++) {
output[i] = 0;
}

return output;
}

const parts = [];
let rest = value;

Expand Down
14 changes: 13 additions & 1 deletion src/splitInteger.test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
'use strict';

const splitInteger = require('./splitInteger');
const splitInteger = require('./splitInteger.js');

test(`should split a number into equal parts
if a value is divisible by a numberOfParts`, () => {
const value = [8, 8, 8, 8, 8, 8, 8, 8];
const recived = splitInteger(64, 8);

expect(value).toEqual(recived);
});

test(`should return a part equals to a value
when splitting into 1 part`, () => {
const value = [9];
const recived = splitInteger(9, 1);

expect(value).toEqual(recived);
});

test('should sort parts ascending if they are not equal', () => {
const value = [5, 6, 6, 6];
const recived = splitInteger(23, 4);

expect(value).toEqual(recived);
});

test('should add zeros if value < numberOfParts', () => {
const value = [0, 0, 0, 0];
const recived = splitInteger(3, 4);

expect(value).toEqual(recived);
});