Queue Spec

Elvenware Logo

TOC

#Queue Specification

On Elvenware, you can find a page that specifies the basic operations performed by Queues and Stacks.

For this assignment, you can use the Simple Queue example from JsObjects as a starter project and a resource. When you it into your Git repository, however, I want you to call the assignment Week03QueueSpec. To complete the submission process, simply give the URL of your Git repository.

Another good resource is the Array All program in JsObjects.

##Part 01 Using a constructor function create your own stack and queue classes. Now write tests that prove the following:

For Queues, create the following tests. For each test create a new instance of your queue and initialize as needed:

    var padNumber = function(numberToPad, width, padValue) {
        'use strict';
        padValue = padValue || '0';
        numberToPad = numberToPad + '';
        if (numberToPad.length >= width) {
            return numberToPad;
        } else {
            return new Array(width - numberToPad.length + 1).join(padValue) + numberToPad;
        }
    };

##Part 02

For Stacks, create the following tests:

    function stripWhiteSpace(value) {
        'use strict';
        return String(value)
            .replace(/ /g, '')
            .replace(/\t/g, '')
            .replace(/\r/g, '')
            .replace(/\n/g, '');    
    }

    function stripPunctuation(value) {
        'use strict';
        return String(value)
            .replace(/\./g, '')
            .replace(/!/g, '')
            .replace(/\?/g, '')
            .replace(/,/g, '');    
    }

Again, you can visit ElfUtils to learn more about these methods.

#Some Hints and Tricks

I'm adding this section for two reasons:

Take exactly as much as you want from these hints. Don't deliberatly make the assignment harder than it already is by trying to take on things like the modular pattern, or properties, that might confuse you. On the other hand, if you see some code in here that helps you understand how your code should look, then please go ahead and use it.

In class, we talked about the Jasmine beforeEach method which can be used in your test code. It is so useful, and so easy to use, that I thought I would give it to you here, in case you want to use it:

describe("A Queue Simple Suite", function() {
    'use strict';

    var simpleQueue = null;

    // This method is called before each test
    beforeEach(function() {
        simpleQueue = new SimpleQueue();
    });

    // Call this method if you want to automatically load certain values
    function loadDefaultValues() {
        simpleQueue.enqueue('Alpha');
        simpleQueue.enqueue('Bravo');
        simpleQueue.enqueue('Charlie');
        simpleQueue.enqueue('Echo');
        simpleQueue.enqueue('Delta');
    }

    it("tests loadDefaults without giving away too many other answers", function() {
        loadDefaultValues();
        var len = simpleQueue.length();
        for (var i = 0; i < len; i++) {
            simpleQueue.dequeue();
        }
        expect(simpleQueue.empty()).toBe(true);
    });

    etc...

To have the beforeEach method called before each of your tests, all you need to do is include it in your tests, as shown above.

Note to beginners: All the code shown above would be found in your test code, probably in the file called QueueSimpleSpec.js.

##Properties

In the code shown above, you might notice that we call length as function: length(). This is a bit awkward. If you want, you can use a property instead:

function SimpleQueue() {
    dataStore = [];

    Object.defineProperty(this, "length", {
        get: function() {
            return dataStore.length;
        },
        enumerable: true,
        configurable: true
    });

    SimpleQueue.prototype.enqueue = function() {
    etc...

The code shown above would be found in your copy of SimpleQueue.js. After adding the property, you can now access the length property in your tests like this:

var len = simpleQueue.length;  // No longer need to call length(). Now its a property.
for (var i = 0; i < len; i++) {
    simpleQueue.dequeue();
}

In the code shown above, we use a relatively new feature of JavaScript called a property. There are several ways to use properties. In this case we define a read-only property implemented primarily in the get function. If you want to jump ahead and see what else can be done, you can visit the Properties folder in JsObjects.

By the way, the code shown above works exactly the same way if you are using the Modular pattern:

var SimpleQueue = (function() {

    var dataStore = null;

    function SimpleQueue() {
        dataStore = [];

        Object.defineProperty(this, "length", {
            get: function() {
                return dataStore.length;
            },
            enumerable: true,
            configurable: true
        });
    }

    SimpleQueue.prototype.enqueue = function() {
    etc...

My tests look exactly the same whether I use a standard constructor function or the modular pattern. (Mike, as mentioned in class on Wednesday, creating properties is another place where it is okay to use this.)

In this assignment, there are other places, such as back and front, where you could use properties. Doing so is optional, but if you want to learn a little more now, rather than later, then go ahead....

Newcomers to JavaScript: Don't worry too much about the Modular pattern unless you are comfortable with it. The main thing for you here is that there are several tips as to how to put together your queue class.

##Video

Watch the video: