Forcing A Constructor In JavaScript

Did you know that you can force a function to only act as a constructor? The following is an example of a Person pseudo-class which can be defined with or without the new keyword:

// Defines a person object.
function Person(firstName, lastName, age) { 
  // If called without parameters.
  if(!arguments.length) {
    // If this is a recursive call, allow the properties to now be defined.
    if(arguments.callee.caller === arguments.callee) {
      return this;
    }
    // If this is a non-recursive call, throw an error since the constructor
    // can't be called without parameters.
    else {
      throw new Error("No parameters were specified for the Person object.");
    }
  }
  
  // Allows make sure this function acts as a constructor.
  var me = !(this instanceof arguments.callee) ? new Person : this;
    
  // Define the properties.
  me.firstName = firstName;
  me.lastName = lastName;
  me.age = age;
  
  // Return a reference to the new Person instance.
  return me;
}

// Define the prototypal functions.
Person.prototype = {
  toString : function() {
    return this.firstName + " " + this.lastName + " is " + this.age
      + " years old.";
  }
};

One of the things that you may have noticed is the fact that I am throwing an error if the constructor is called without any arguments. If you don’t want that to happen, you can simply remove that else statement. Everything is probably pretty self explanatory.

The following are two examples which prove the above code actually works:

// Create an instance of a person without the "new" keyword.
var p = Person("Chris", "West", 23);
alert(p.firstName);  // display "Chris"
alert(p instanceof Person);  // display true
alert(p);  // use toString() function

// Create an instance of a person with the "new" keyword.
var p = new Person("Tamara", "Thomas", 21);
alert(p.age);  // display 21
alert(p instanceof Person);  // display true
alert(p);  // use toString() function.

POW – Quick Escape

This is the first of a new series of posts called the “Problem of the Week” (POW). As is evidenced by the title, these problems will be posted weekly. Every Sunday, I will post the answer to the previous week’s POW.

Problem

You are a member on a team of four people competing against five other teams. Each person gets a distinct number which represents how many minutes it will take that person to get across a bridge. Person A can go across in at least 1 minute, person B in 2 minutes, person C in 5 minutes, and person D in 10 minutes. Your team, just as the others, has one flashlight to share between one or two people as you cross the bridge. One of the people crossing must have the flashlight in hand. The goal is to get everyone from one side of the bridge, to the other in less time than the other teams did. The time to beat is 19 minutes. The reason it takes so long is because two people can only cross the bridge at the speed of the slowest individual. Also, the flashlight may not be thrown to someone to cut down on time. Is it possible for your team to get everyone across the bridge in less than 19 minutes? If so, how can you do it?

Example Run

  1. Person A (1 minute) and person D (10 minutes) go across to the target side with the flashlight in 10 minutes.
  2. Person A (1 minute) comes back across the bridge with the flashlight in one minute, bringing the total time to 11 minutes.
  3. Person A (1 minute) and person C (5 minutes) go across to the target side with the flashlight in 5 minutes, bringing the total time to 16 minutes.
  4. Person A (1 minute) comes back across the bridge with the flashlight in one minute, bringing the the total time to 17 minutes.
  5. Person A (1 minute) and person B (2 minutes) go across to the target side with the flashlight in 2 minutes, bringing the final time to 19 minutes.

Divisibility Rules for 11 and 12

On Wednesday I explained how to determine if a number is divisible by 3, 6, and/or 9. I also posted a comment with a link to rules for 2, 3, 4, 5, 6, 7, 8, 9, and 10. Therefore, I think it is time for 11 and 12.

Divisibility Rule for 11

Let’s say that we want to determine if 31823 is evenly divisible by 11.  I will know that it is evenly divisible by 11 if 3 – 2 + 8 – 1 + 3 is evenly divisible by 11.  31823 is divisible by 11 because 3 – 2 + 8 – 1 + 3 is equal to 11.

Let’s try 9123:  3 – 2 + 1 – 9 = -7.  Since -7 is not divisible by 11, 9123 is not evenly divisible by 11.

Let’s try one more together to make sure you understand the pattern.  Is 9876543210 divisible by 11?  Since 0 – 1 + 2 – 3 + 4 – 5 + 6 – 7 + 8 – 9 = -5 and -5 is not evenly divisible by 11, 9876543210 is not evenly divisible by 11.

Okay, so now that we understand the pattern, how do we find a nearby number that is divisible by 11?  Believe it or not, you just have to subtract the number that you get from our math problem to the original number, to get one that is divisible by 11.

Let’s use 9123.  3 – 2 + 1 – 9 gave us -7.  Using that number, we can do the following:  9123 – (-7) = 9123 + 7 = 9130.  This means that 9130 is evenly divisible by 11.

Now let’s try our other number that wasn’t divisible by 11:  9876543210.  We will subtract -5 which came from 0 – 1 + 2 – 3 + 4 – 5 + 6 – 7 + 8 – 9.  Using that number, we will find the answer by doing this:  9876543210 – (-5) = 9876543210 + 5 = 9876543215.  That means that 9876543215 is evenly divisible by 11.

Divisibility Rule for 12

A number is divisible by 12 if it is divisible by both 3 and 4.  The reason this is true is because 12 = 3 × 4.  Knowing this, the only part that you may not understand is when a number is evenly divisible by 4.  This is the case when the last two digits of the number are divisible by four.

If you have trouble looking at a two digit number and figuring out if it is divisible by four, try the following:

  1. If the right-most digit is odd, it is not divisible by four.
  2. If the right-most digit is 0, 4, or 8, the next digit to the left must be even.
  3. If the right-most digit is 2, or 6, the next digit to the left must be odd.

As I explained in my previous divisibility post, a number is evenly divisible by 3 only if the sum of the digits in the entire number is even divisible by three.