Valid Number
One last challenge! It's been a long one in a way.
Today's challenge was centred around writing a logic that returns true when a number is valid and false when it's not valid. The question gave some basic rules a valid number should follow, so that will be put into consideration as the logic is been written
Thought process
Looking at the question, It was a bit straightforward.
These were my thoughts:
use the
trim()
method to clear the string of whitespaces.Convert the given string to a number.
Check if the result from the conversion gave
NaN
or not.Return false from the function if the conversion gave
NaN
and true if it doesn't.
Implementation
I implemented exactly the way my thought process was and guess what? It was a wrong answer. For some reason the text Infinity
was passing the test and returning true. To battle this I created a condition that checks if the input includes Infinity
and then return false. Might not be the best solution but that gave me a pass for now.
What I learnt
Today's task was more of understanding the question better and interpreting it in the best possible way. Was also able to revise some basic JavaScript methods
Conclusion
I'm excited that I was able to stay on this challenge this long and finish. This is a big thing for me and I want to thank I4G for this opportunity to participate in this challenge
Thanks to those that were with me and read my articles from the start of the challenge till now.
Thank you for reading through
Challenge done!
You can follow me on Twitter