Tag: telephone number

Frustrated

Frustrated

So I built this JavaScript basic calculator a month or two ago.  I have to admit there was a good bit of “reverse engineering” involved but I am still improving it…or so that was the plan.  Between working full-time nights in a non tech related field to support the family, looking for a job IN a tech related field and even  my daughter’s spring break, I haven’t had much time to work on it until today.  That’s when it hit me.  I’ve forgotten things.  Parts that came easy when I was able to work daily on coding became more cumbersome.  Not having the time to spend coding has caused me to forget some of what I’d learned. I’ve still be able to listen to podcasts every night.  I’ve even been working on things related to getting a job like Agile development, Scrum, SASS, RESTful APIs, web services, and more.  But, I haven’t been actively coding.

Now, I can’t say that I’d forgotten everything.  The basics of html, css and javascript are still in my brain somewhere. I just need to find out where.  But, how?  Do I go back to the basics and start learning from scratch again?  That seems like a lot of wasted time as I still know most of the material.  I went back to the basic algorithm scripting problems. I can still reverse a string with split(), reverse() and join().  I can check a string for palindromes by making everything lowercase, getting rid of any punctuation, then loop through the string comparing the first and last letters as I go. Great!  Got that down.

What about Intermediate scripting? I can sum all prime numbers using % (modulus). I can flatten a nested array and I can convert a string to “spinal” case.  I can even validate telephone numbers with regular expressions (which I kind of covered in a previous blog post).

So, what’s wrong?  The obvious answer is to spend more time coding, building, and making things.  Listening to podcasts and courses from Lynda are great but they’re only part of the picture.  I have a limited amount of time in the day and I need to schedule dedicated time to just building a specific project.  I’ve had a tendency to take online classes and it’s mostly stuff I know or can figure out.  These kinds of things give me an immediate reward because I can do them in a reasonable amount of time and I get positive reinforcement right away. I feel good when I solve a problem but in the long run, I’m not getting much further along in putting everything together.

I am going to pick one project like building a tic-tac-toe or simon style game and spend at least one scheduled hour daily working on it.  At night, I’ll still listen to podcasts but when I get computer time it will be to complete one project and not rely on as much “reverse engineering” as I did with the calculator.  I’ll keep you all updated on my progress.  Thanks for reading!

(Now, what editor do I use?  Brackets? Visual Studio? ….)

 

“If at first you don’t succeed; call it version 1.0”

Express Regularly!

Part 1

One recent lesson involved Regular Expressions or RegEx.  RegEx allows you to quickly scan over text for different properties.  Regular Expressions almost seem like an entire language by itself and is does exist in more than just JavaScript.  The idea has actually been around since the 1950s and you can find it languages like Python and Perl.

Here’s a real quick overview of the basic concepts…

The first, simplest match would be just a character.  Like A,b,z, or 3.  We mark the beginning and end of the RegEx with the forward slash /.  So, say I was looking for the letter “a” so I would use /a/ to look for it.

regex1

Cool, so  /a/ matches the “a” in the middle of “cat”…now what?

If we want to look for a range of characters, we can use the “-” to define a range.  If we want to check for a three letter word that starts with “c” we can define the first character with /c/ and the remaining three with ranges [a-z][a-z].

regex2

(By the way, the “g” at the end just turns on the “global” feature.  If this were off, it would match the very first word and stop.  This just lets us check the whole list at once.)

Here we match the “c” in “cat” but not the “b” in “bat” and the “c” in “car”.  As long as the last two characters are in the range “a” through “z”, it will match.

You can use the same method to match numbers, BUT…there is one little thing you have to remember.  When using RegEx with numbers, you’re actually looking for the numbers as a text character and not as actual numbers!  If you set up a range like 1 through 10 you’re not actually looking for the numbers 1 to 10.  You’re looking for the characters 1 through 1 and 0!  For example if you search on the range of 1-10 you’ll match all the 1’s and all the 0’s.  You can see below how this matches up to different numbers.

regex3

Just remember, you’re searching for the characters, not the numbers!

So, in Code Camp, the first problem to solve with Regular Expressions was to validate US telephone numbers. We’re looking for something that looks like 555-555-5555 or (555)555-5555.  A number like 52-55-8888 should give us an incorrect response.

Using what we know about ranges and how numbers are considered characters, we can first start looking at each number to check if it’s in the right format. (We’re going to ignore the dashes and braces for right now, we’ll address that a little later.)  At first glance, it looks like we have to set up a search for every number individually.  You might be thinking of something like

regex4

This method works to look for sets of three numbers in a row but there has to be a better way, right?

Thankfully, yes.  There are shorthand characters we can use instead of typing out every number!  For numbers, we can use the “Digit” shorthand character “\d”. We can use “\d” as shorthand for [1234567890]. This will save us a TON of typing! Now, the RegEx looks something like this.

regex5

See how we were able to replace [1234567890] with just a “\d”?  There is also another shortcut we can use here that will (believe it or not) make it even better!  We don’t even have to use “\d” three times!  We can just do it once and then specify we want to repeat it how ever many times we want!

regex6

We just use the “\d” and then the “{3}” tells it to look for the digits three more times!

This seems like a good place to stop for the moment.  In my next post, we’ll put all this together to check those US Telephone Numbers!  Thanks for reading!

“My software never has bugs. It just develops random features.”-Unknown