Skip to content

bug: abs returns 0 on an empty array #1473

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

Merged
merged 3 commits into from
Oct 10, 2023
Merged
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
4 changes: 2 additions & 2 deletions Maths/Abs.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
const abs = (num) => {
const validNumber = +num // converted to number, also can use - Number(num)

if (Number.isNaN(validNumber)) {
if (Number.isNaN(validNumber) || typeof num === 'object') {
throw new TypeError('Argument is NaN - Not a Number')
}

return validNumber < 0 ? -validNumber : validNumber // if number is less then zero mean negative then it converted to positive. i.e -> n = -2 = -(-2) = 2
return validNumber < 0 ? -validNumber : validNumber // if number is less than zero mean negative then it converted to positive. i.e -> n = -2 = -(-2) = 2
}

export { abs }
1 change: 1 addition & 0 deletions Maths/test/Abs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ describe('Testing abs function', () => {
expect(() => abs('234a')).toThrow()
expect(() => abs({})).toThrow()
expect(() => abs([12, -32, -60])).toThrow()
expect(() => abs([])).toThrow() // coerces to 0
})

it('Testing for number of string type', () => {
Expand Down