Javascript skill evaluation
Mateusz
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 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._prevValue=0;
this._current=1;
}
window.MyFunction.prototype.current = function(){
return this._current;
}
window.MyFunction.prototype.increment = function(){
this._current+=this._prevValue;
this._prevValue = this._current-this._prevValue;
return this;
}
window.MyFunction.prototype.decrement = function(){
var diffToPrev=this._current-this._prevValue;
this._current=this._prevValue;
this._prevValue = diffToPrev;
return this;
}