DEV Community

Discussion on: Negative Number to Positive Number without using abs() - asked in a interview

Collapse
aarone4 profile image
Aaron Reese

Why?
Under what circumstances would you NOT want to use abs()? If I saw that function name I would be assuming there was an edge case that abs on its own didn't handle

Collapse
rakshithholla profile image
Rakshith holla Author

Hi,

Sorry, I missed to add that this was asked in a interview. abs() works fine.

Collapse
aarone4 profile image
Aaron Reese

So another answer would be to convert it to a string, replace "-" with "" and convert back to a number.