Javascript skill evaluation

Cezary

by Alexandre Azevedo

HTML

<script src="https://searls.github.io/jasmine-all/jasmine-all-min.js"></script>
<script type="application/javascript">
	'use strict';

    describe('Declaration', function() {
		it('is object', function() {
        	var type = typeof(window.MyFunction);
            expect(type).toBe('function');
        });
    });

    describe('Execution of', function() {
    
    	var value;

        it("initial sequence", function() {
	        value = new window.MyFunction();
            expect(value.current()).toBe(1);
        });

		it("incrementing sequence once", function() {
	        value.increment();
            expect(value.current()).toBe(1);
        });

		it("incrementing sequence three times", function() {
	        value.increment().increment().increment().increment();
            expect(value.current()).toBe(8);
        });

		it("decrementing sequence two times", function() {
	        value.decrement().decrement();
            expect(value.current()).toBe(3);
        });
    });

</script>

JavaScript

'use strict';

window.MyFunction = function(){
	this.previousValue = 0;
	this.currentValue = 1;
};

window.MyFunction.prototype.current = function(){
	return this.currentValue;
};

window.MyFunction.prototype.increment = function(){
	var previous = this.previousValue;
	this.previousValue = this.currentValue;
	this.currentValue += previous;
	return this;
};

window.MyFunction.prototype.decrement = function(){
	var previous = this.previousValue;
	this.previousValue = this.currentValue - previous;
	this.currentValue -= this.previousValue;
	return this;
};