Skip to content
Open

Develop #1022

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 package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"test:only": "mate-scripts test",
"update": "mate-scripts update",
"postinstall": "npm run update",
"test": "npm run lint && npm run test:only"
"test": "jest"
},
"author": "Mate academy",
"license": "GPL-3.0",
Expand All @@ -19,7 +19,7 @@
"eslint": "^5.16.0",
"eslint-plugin-jest": "^22.4.1",
"eslint-plugin-node": "^8.0.1",
"jest": "^24.5.0"
"jest": "^24.9.0"
},
"mateAcademy": {
"projectType": "javascript"
Expand Down
73 changes: 73 additions & 0 deletions src/splitInteger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,91 @@ const splitInteger = require('./splitInteger');

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

expect(res).toEqual([2, 2, 2, 2]);
expect(res).toHaveLength(4);
expect(res.reduce((a, b) => a + b, 0)).toBe(8);
expect(res.every(Number.isInteger)).toBe(true);
expect(res).toEqual(res.slice().sort((a, b) => a - b));
expect(Math.max(...res) - Math.min(...res)).toBeLessThanOrEqual(1);
});

test(`should return a part equals to a value
when splitting into 1 part`, () => {
const res = splitInteger(8, 1);

expect(res).toEqual([8]);
expect(res).toHaveLength(1);
expect(res.reduce((a, b) => a + b, 0)).toBe(8);
expect(res.every(Number.isInteger)).toBe(true);
expect(res).toEqual(res.slice().sort((a, b) => a - b));
expect(Math.max(...res) - Math.min(...res)).toBeLessThanOrEqual(1);
});

test('should sort parts ascending if they are not equal', () => {
const res = splitInteger(10, 3);

expect(res).toEqual([3, 3, 4]);
expect(res).toHaveLength(3);
expect(res.reduce((a, b) => a + b, 0)).toBe(10);
expect(res.every(Number.isInteger)).toBe(true);
expect(res).toEqual(res.slice().sort((a, b) => a - b));
expect(Math.max(...res) - Math.min(...res)).toBeLessThanOrEqual(1);
});

test('should add zeros if value < numberOfParts', () => {
const res = splitInteger(3, 5);

expect(res).toEqual([0, 0, 1, 1, 1]);
expect(res).toHaveLength(5);
expect(res.reduce((a, b) => a + b, 0)).toBe(3);
expect(res.every(Number.isInteger)).toBe(true);
expect(res).toEqual(res.slice().sort((a, b) => a - b));
expect(Math.max(...res) - Math.min(...res)).toBeLessThanOrEqual(1);
});

// 🔹 Додаткові приклади з опису завдання
test('should split 6 into 2 equal parts', () => {
const res = splitInteger(6, 2);

expect(res).toEqual([3, 3]);
expect(res).toHaveLength(2);
expect(res.reduce((a, b) => a + b, 0)).toBe(6);
expect(res.every(Number.isInteger)).toBe(true);
expect(res).toEqual(res.slice().sort((a, b) => a - b));
expect(Math.max(...res) - Math.min(...res)).toBeLessThanOrEqual(1);
});

test('should split 17 into 4 nearly equal parts', () => {
const res = splitInteger(17, 4);

expect(res).toEqual([4, 4, 4, 5]);
expect(res).toHaveLength(4);
expect(res.reduce((a, b) => a + b, 0)).toBe(17);
expect(res.every(Number.isInteger)).toBe(true);
expect(res).toEqual(res.slice().sort((a, b) => a - b));
expect(Math.max(...res) - Math.min(...res)).toBeLessThanOrEqual(1);
});

test('should split 32 into 6 nearly equal parts', () => {
const res = splitInteger(32, 6);

expect(res).toEqual([5, 5, 5, 5, 6, 6]);
expect(res).toHaveLength(6);
expect(res.reduce((a, b) => a + b, 0)).toBe(32);
expect(res.every(Number.isInteger)).toBe(true);
expect(res).toEqual(res.slice().sort((a, b) => a - b));
expect(Math.max(...res) - Math.min(...res)).toBeLessThanOrEqual(1);
});

test('should split when value equals numberOfParts', () => {
const res = splitInteger(5, 5);

expect(res).toEqual([1, 1, 1, 1, 1]);
expect(res).toHaveLength(5);
expect(res.reduce((a, b) => a + b, 0)).toBe(5);
expect(res.every(Number.isInteger)).toBe(true);
expect(res).toEqual(res.slice().sort((a, b) => a - b));
expect(Math.max(...res) - Math.min(...res)).toBeLessThanOrEqual(1);
});