Marcus Pope

Husband, Father, Inventor, Programmer Austin, TX - °

Code Samples

The following samples use JavaScript and VerbotenJS to solve the programming problems I've come across online. Nothing really complex, but enough to demonstrate my ability to code as well as the utility of the library I wrote. I've only done a couple so far, I'll keep practicing with more while I'm interviewing.

Sunday, Sunday, Sunday!

For the years 1901 to 2000, count the total number of Sundays that fell on the first of a month.

require('verbotenjs');

invoke(function() {
    var t1 = new Date('12/31/2000');
    var count = 0;

    while(t1.getYear() >= 1901) {
        //look for sundays on the first of the month
        if (t1.getDOWName() == "Sunday") {
            if (t1.getDayNum() == 1) {
                count++;
            }
            t1 -= 7..days();
        }
        else {
            t1 -= 1..days();
        }
    }

    alert(count);
});

Output: 171

~~~~~~~~~~~ 001100010010011110100001101101110011 ~~~~~~~~~~~

Spiralling Square

Starting at the upper left corner of the square and walk its edges clockwise. Just before re-approaching the upper left corner, spiral into the square instead, ultimately arriving at the center of the square.

require('verbotenjs');

invoke(function() {

    //aliases for the rect and coord array indexes
    var x = 0, y = 1, w = 2, h = 3;

    //10-cell grid
    var rect = [0,0,9,9];

    //start in first cell
    var coord = [rect[x], rect[y]];
    alert(coord);

    var last = coord.join("");
    while(true) {
        //traverse grid according to rules in question
        //@note: this is a good example where duplicate logic is more
        //maintainable than a clean abstraction
        if (coord[x] < rect[w] - 1) {
            while(coord[x] < rect[w] - 1) { coord[x]++; alert('right'); }
            rect[y]++;
        }
        if (coord[y] < rect[h] - 1) {
            while(coord[y] < rect[h] - 1) { coord[y]++; alert('down'); }
            rect[w]--;
        }
        if (coord[x] > rect[x]) {
            while(coord[x] > rect[x]) { coord[x]--; alert('left');}
            rect[h]--;
        }
        if (coord[y] > rect[y]) {
            while(coord[y] > rect[y]) { coord[y]--; alert('up');}
            rect[x]++;
        }

        //if we stop moving bail
        if (coord.join("") == last) break;
        last = coord.join("");
    }

    alert('done');
    alert(coord);
});