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
23 changes: 23 additions & 0 deletions .github/workflows/test.yml-template
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Test

on:
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules
/dist
8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"trailingComma": "all",
"printWidth": 80
}
Binary file added src.zip
Binary file not shown.
47 changes: 34 additions & 13 deletions src/splitInteger.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,44 @@
// 'use strict';

// /**
// * @param {number} value
// * @param {number} numberOfParts
// *
// * @returns {number[]}
// */
// function splitInteger(value, numberOfParts) {
// const parts = [];
// let rest = value;

// for (let partsLeft = numberOfParts; partsLeft > 0; partsLeft--) {
// const part = Math.floor(rest / partsLeft);

// parts.push(part);
// rest -= part;
// }

// return parts;
// }

// module.exports = splitInteger;

'use strict';

/**
* @param {number} value
* @param {number} numberOfParts
*
* @returns {number[]}
*/
function splitInteger(value, numberOfParts) {
const parts = [];
let rest = value;
const base = Math.floor(value / numberOfParts);
const remainder = value % numberOfParts;
const result = Array(numberOfParts).fill(base);

for (let partsLeft = numberOfParts; partsLeft > 0; partsLeft--) {
const part = Math.floor(rest / partsLeft);
for (let i = 0; i < remainder; i++) {
result[numberOfParts - 1 - i] += 1;
}

parts.push(part);
rest -= part;
// If value < numberOfParts, fill with zeros and ones
if (value < numberOfParts) {
return Array(numberOfParts - value).fill(0).concat(Array(value).fill(1));
}

return parts;
return result;
Comment on lines +28 to +41

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: The logic for distributing the base and remainder is executed before checking if value < numberOfParts. This causes incorrect results when value < numberOfParts, as the function will fill the array with zeros and ones only after already modifying the result array. To fix this, move the if (value < numberOfParts) block before the calculation of base, remainder, and result.

}

module.exports = splitInteger;
8 changes: 4 additions & 4 deletions src/splitInteger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ const splitInteger = require('./splitInteger');

test(`should split a number into equal parts
if a value is divisible by a numberOfParts`, () => {

expect(splitInteger(8, 2)).toEqual([4, 4]);
});

test(`should return a part equals to a value
when splitting into 1 part`, () => {

expect(splitInteger(8, 1)).toEqual([8]);
});

test('should sort parts ascending if they are not equal', () => {

expect(splitInteger(7, 2)).toEqual([3, 4]);
});

test('should add zeros if value < numberOfParts', () => {

expect(splitInteger(3, 4)).toEqual([0, 1, 1, 1]);
});