Skip to content
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