Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

chore: update abs function #1472

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

Closed
ridge-kimani wants to merge 2 commits into TheAlgorithms:master from ridge-kimani:update-abs-fn
Closed
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
9 changes: 4 additions & 5 deletions Maths/Abs.js
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
*/

const abs = (num) => {
const validNumber = +num // converted to number, also can use - Number(num)
const result = Math.abs(num)

if (Number.isNaN(validNumber)) {
throw new TypeError('Argument is NaN - Not a Number')
}
if (Number.isNaN(result))
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 result
}

export { abs }

AltStyle によって変換されたページ (->オリジナル) /