Absolute in TypeScript

Example of Absolute use

Today we discuss Absolute

To return a number here is a very complicated task because a number of iterations is limited (if we try to solve it with arrays). Instead we can use string literals and return a string as a result.

Here we go ๐Ÿš€

Return result for the string

If we have a string, and we know that thereโ€™s a number in it, we can check for a minus sign at the beginning:

Positive in case we have negative

Otherwise, we return the same string as we expect it to be positive

https://tsplay.dev/WKko8W

Return result for the number and bigint

Still we return the same number or bigint if we pass it to Absolute

As we already can manipulate numbers in a string, letโ€™s put a number to a string:

Put number and bigint into a string

And now all the tests are passed โœ…ย โ€“ https://tsplay.dev/WJ9oRm

Good night โ›ˆ๐ŸŒ™

typescriptmedium

Let's chat and have fun ๐Ÿง‘โ€๐Ÿ’ป๐Ÿ‘ฉโ€๐Ÿ’ป

No more often than once a week I write ๐Ÿ“„ about TypeScript ๐Ÿ’ช, video playback ๐Ÿ“บ and frontend related topics. You can unsubscribe in any time โ†˜๏ธ

ยฉ 2021, Built with Gatsby