DEV Community

Discussion on: Help me with the code, most of the test cases passed but when a negative number encounter it return its positive value.

Collapse
 
jrogers8835 profile image
jrogers8835

When you convert to a string the '-' loses context. You would need ^[-+]?\d*$ to allow the negative sign. This doesn't work for decimals btw. Not sure if you want those.

Collapse
 
strangelestrade profile image
SHivam Sinha

still not working
gives an unexpected error.
Help

Collapse
 
jrogers8835 profile image
jrogers8835

I did mention it doesn't work for decimals. Regex is useful. Use this site for testing and see what you can find from google to account for decimals.
regexr.com/
Hint: '.' Is a special character