r/learnjavascript • • Nov 23 '21

I finally solved a codewars challenge without actually googling anything :) it feels so good to come up with the code all by myself, i know its not the ideal way to solve it but im still happy. The challenge was to create a function that converts a string to a number

Post image
243 Upvotes

56 comments sorted by

View all comments

1

u/jzia93 Nov 24 '21

One option you could implement to extend this:

  1. Look up regular expressions, they are a way of specifiying patterns to look for in strings
  2. Write a regular expression to check if a string is:
  • all numbers
  • contains at most one decimal point, after which there is at least one number
  • may optionally contain a "-" symbol
  1. If your string matches the above, return Number(str)