Pages

Saturday, July 23, 2016

Coding Challenge: Telephone number check

Oh, I know it's boring and horrible, but let's make a function that verifies the telephone number entered by the user is in a correct format.

Here are the rules for a correct format:

telephoneCheck("555-555-5555") should return true.
telephoneCheck("1 555-555-5555") should return true.
telephoneCheck("1 (555) 555-5555") should return true.
telephoneCheck("5555555555") should return true.
telephoneCheck("555-555-5555") should return true.
telephoneCheck("(555)555-5555") should return true.
telephoneCheck("1(555)555-5555") should return true.
telephoneCheck("555-5555") should return false.
telephoneCheck("5555555") should return false.
telephoneCheck("1 555)555-5555") should return false.
telephoneCheck("1 555 555 5555") should return true.
telephoneCheck("1 456 789 4444") should return true.
telephoneCheck("123**&!!asdf#") should return false.
telephoneCheck("55555555") should return false.
telephoneCheck("(6505552368)") should return false
telephoneCheck("2 (757) 622-7382") should return false.
telephoneCheck("0 (757) 622-7382") should return false.
telephoneCheck("-1 (757) 622-7382") should return false
telephoneCheck("2 757 622-7382") should return false.
telephoneCheck("10 (757) 622-7382") should return false.
telephoneCheck("27576227382") should return false.
telephoneCheck("(275)76227382") should return false.
telephoneCheck("2(757)6227382") should return false.
telephoneCheck("2(757)622-7382") should return false.
telephoneCheck("555)-555-5555") should return false.
telephoneCheck("(555-555-5555") should return false.
telephoneCheck("(555)5(55?)-5555") should return false.


If a country code is used, it must be 1.  

Good luck.  Solution is in the comments.  

Monday, July 11, 2016

Yes you can use CSS to post an image

background-image:url(http://whateverthepictureurlis.com);


This is cool because you can make a div have a background of the picture, then you can easily type on top of it and not have to worry about responsiveness.


Sunday, July 10, 2016

Daddy Object

The Big Daddy of all in Javascript is Object.prototype

Once upon a time daddy was created:

var daddy = {
  apples : 10
}

Daddy has only one variable, apples with a value of ten.

daddy;  in a repl will return {apples: 10}

Daddy actually has more properties than just the one property of apple, you just can't see them, they've been inherited from the Big Daddy of all objects, the Object.prototype.  Big Daddy Object.prototype contains methods that all children objects have access to.

So, daddy was created from Big Daddy, and then one say daddy had a son, name son object;

var son = Object.create(daddy);

Booooom, son is born.

son;   run in a repl will return {};

This is because it's an empty object.  However son.apples; run in a repl will return 10.

When the browser looks for a property named apples in son, and doesn't find one, it looks up the chain, the Big Daddy chain, or the prototype chain, to the next in line, which is daddy.  Booom, inside daddy it finds a variable named apples, and so returns 10.

Wednesday, July 6, 2016

_.each, .forEach

I find it really frustrating that the native Javascript forEach does not iterate through objects.

The underscore each does, and I haven't used it, but apparently the jQuery each also does.

So what's a guy to do if you're too lazy to implement the underscore library.

Well, I suppose this will work for an object named obj:

Object.keys(obj).forEach(function(item){
console.log(obj[item]);
})

I'll probably update this in the future when I am smarter.