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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# QA: Split integer ([uk](readme.uk.md))
# QA: Split integer ([uk](https://github.com/AndrzejFront/js_split_integer/blob/main/readme.uk.md))

Write tests for `splitInteger` function that takes 2 positive integers `value`
and `numberOfParts` and returns an array containing exactly `numberOfParts`
Expand Down
51 changes: 47 additions & 4 deletions src/splitInteger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,54 @@
const splitInteger = require('./splitInteger');

test(`should split a number into equal parts
if a value is divisible by a numberOfParts`, () => {});
if a value is divisible by a numberOfParts`, () => {
expect(splitInteger(6, 2)).toEqual([3, 3]);
});

test(`should return a part equals to a value
when splitting into 1 part`, () => {});
when splitting into 1 part`, () => {
expect(splitInteger(8, 1)).toEqual([8]);
});

test('should sort parts ascending if they are not equal', () => {});
test('should sort parts ascending if they are not equal', () => {
expect(splitInteger(17, 4)).toEqual([4, 4, 4, 5]);
expect(splitInteger(32, 6)).toEqual([5, 5, 5, 5, 6, 6]);
expect(splitInteger(29, 3)).toEqual([9, 10, 10]);
});

test('should add zeros if value < numberOfParts', () => {});
test('should add zeros if value < numberOfParts', () => {
expect(splitInteger(3, 5)).toEqual([0, 0, 1, 1, 1]);
});

test('should split the smallest positive value', () => {
expect(splitInteger(1, 1)).toEqual([1]);
expect(splitInteger(1, 4)).toEqual([0, 0, 0, 1]);
});

test('should return ones if value equals numberOfParts', () => {
expect(splitInteger(4, 4)).toEqual([1, 1, 1, 1]);
});

test('should return exactly numberOfParts elements', () => {
expect(splitInteger(23, 7)).toHaveLength(7);
});

test('should preserve the total value', () => {
const parts = splitInteger(23, 7);

expect(parts.reduce((sum, part) => sum + part, 0)).toBe(23);
});

test('should return only integer parts', () => {
const parts = splitInteger(23, 7);

parts.forEach((part) => {
expect(Number.isInteger(part)).toBe(true);
});
});

test('should keep the difference between max and min parts at most 1', () => {
const parts = splitInteger(23, 7);

expect(Math.max(...parts) - Math.min(...parts)).toBeLessThanOrEqual(1);
});
Loading