Javascript skill evaluation
Correct answer
by Alexandre Azevedo
HTML
<script src="//searls.github.io/jasmine-all/jasmine-all-min.js"></script>
<script type="application/javascript">
'use strict';
describe('Declaration', function() {
it('is function', 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._previous = 0;
this._current = 1;
};
window.MyFunction.prototype.current = function() {
return this._current;
};
window.MyFunction.prototype.increment = function() {
this._current += this._previous;
this._previous = this._current - this._previous;
return this;
};
window.MyFunction.prototype.decrement = function() {
this._previous = this._current - this._previous;
this._current -= this._previous;
return this;
};