-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Feature/custom test #1058
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Feature/custom test #1058
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| 'use strict'; | ||
|
|
||
| function toBeSortedAscending(arr, number, parts) { | ||
| if (hasRemainder(number, parts) === false) { | ||
| return arr.every((val, i) => i === 0 || arr[i - 1] <= val); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| function hasRemainder(number, parts) { | ||
| return number % parts === 0; | ||
| } | ||
|
|
||
| function hasSpilitEqual(result, number, parts) { | ||
| const remainder = hasRemainder(number, parts); | ||
| const length = result.length === parts; | ||
| const equality | ||
| = result[0] === result[result.length - 1] | ||
| && result.every((e) => e === result[0]); | ||
|
|
||
| return remainder ? (length ? equality : false) : false; | ||
| } | ||
|
|
||
| function hasEqualValueToNumber(result, number) { | ||
| return result[0] === number ? result : false; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This helper makes the test that uses it more complex than necessary. A direct assertion like |
||
| } | ||
|
|
||
| function zerosCounts(number, parts) { | ||
| return Math.max(0, parts - number); | ||
| } | ||
|
|
||
| function howManyZerosInArray(result) { | ||
| return result.reduce((acc, val) => (val === 0 ? acc + 1 : acc), 0); | ||
| } | ||
|
|
||
| module.exports = { | ||
| toBeSortedAscending, | ||
| hasSpilitEqual, | ||
| hasEqualValueToNumber, | ||
| zerosCounts, | ||
| howManyZerosInArray, | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,36 @@ | ||
| 'use strict'; | ||
|
|
||
| const { | ||
| // toBeSortedAscending, | ||
| // hasSpilitEqual, | ||
| hasEqualValueToNumber, | ||
| // zerosCounts, | ||
| // howManyZerosInArray, | ||
| } = require('./customs'); | ||
|
|
||
| const splitInteger = require('./splitInteger'); | ||
|
|
||
| test(`should split a number into equal parts | ||
| 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`, () => { | ||
| const result = splitInteger(8, 1); | ||
| const resultOfCustoms = hasEqualValueToNumber(result, 8); | ||
|
|
||
| expect(resultOfCustoms).toEqual([8]); | ||
|
Comment on lines
+20
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great job simplifying the other tests to use |
||
| }); | ||
|
|
||
| test('should sort parts ascending if they are not equal', () => { | ||
|
|
||
| // const result = splitInteger(17, 4); | ||
| // expect(toBeSortedAscending(result)).toBe(true); | ||
| expect(splitInteger(17, 4)).toEqual([4, 4, 4, 5]); | ||
| }); | ||
|
|
||
| test('should add zeros if value < numberOfParts', () => { | ||
|
|
||
| // const result = howManyZerosInArray(splitInteger(2, 4)); | ||
| // expect(zerosCounts(2, 4)).toBe(result); | ||
| expect(splitInteger(2, 4)).toEqual([0, 0, 1, 1]); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part of the logic is flawed because it unconditionally returns
truewhen a number is evenly divisible, without actually checking if the array is sorted. It's great that you're no longer using this function, and removing it entirely would be the best solution.