Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a set of extra credit array-related tasks that require the user to reimplemen… #128

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions app/arraysExtraCredit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
exports = (typeof window === 'undefined') ? global : window;

/*Pay special attention to what each function is supposed to return
as well as what the .length property of each array should be*/
exports.arraysExtraCreditAnswers = {

indexOf : function(arr, item) {

},

push : function(arr,item) {

},

pop : function(arr) {

},

unshift : function(arr,item) {

},

shift : function(arr) {

},

concat : function(arr1, arr2) {

}
72 changes: 72 additions & 0 deletions tests/app/arraysExtraCredit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
if ( typeof window === 'undefined' ) {
require('../../app/arraysExraCredit');
var expect = require('chai').expect;
}

xdescribe('arraysExtraCredit', function() {
var a;

/*create an array-like object (subclass of Array) that is missing the methods that
the user needs to implement */
var ArrayLike = function() {
Array.call(this);
}
ArrayLike.prototype = Object.create(Array.prototype);
ArrayLike.prototype.indexOf = undefined;
ArrayLike.prototype.push = undefined;
ArrayLike.prototype.pop = undefined;
ArrayLike.prototype.unshift = undefined;
ArrayLike.prototype.shift = undefined;
ArrayLike.prototype.concat = undefined;

beforeEach(function() {
a = new ArrayLike();
a = [ 1, 2, 3, 4 ];
});

it('you should be able to implment indexOf from scratch', function() {
expect(arraysExtraCreditAnswers.indexOf(a, 3)).to.eql(2);
expect(arraysExtraCreditAnswers.indexOf(a, 5)).to.eql(-1);
});

it('you should be able to implement push from scratch', function() {
var result = arraysExtraCreditAnswers.push(a, 10);

expect(result).to.eql(5)
expect(a).to.have.length(5);
expect(a[a.length - 1]).to.eql(10);
});

it('you should be able to implement pop from scratch', function() {
var result = arraysExtraCreditAnswers.pop(a);

expect(result).to.eql(4);
expect(a).to.have.length(3);
expect(a.join(' ')).to.eql('1 2 3');
});

it('you should be able to implement unshift from scratch', function () {
var result = arraysExtraCreditAnswers.unshift(a, 10);

expect(result).to.eql(5);
expect(a).to.have.length(5);
expect(a[0]).to.eql(10);
});

it('you should be able to implement shift from scratch', function () {
var result = arraysExtraCreditAnswers.shift(a);

expect(result).to.eql(1);
expect(a).to.have.length(3);
expect(a.join(' ')).to.eql('2 3 4');
});

it('you should be able to implement concat from scratch', function() {
var c = [ 'a', 'b', 'c', 1 ];
var result = arraysExtraCreditAnswers.concat(a, c);

expect(result).to.have.length(8);
expect(result.join(' ')).to.eql('1 2 3 4 a b c 1');
});

});
2 changes: 2 additions & 0 deletions tests/runner.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
<!-- load tests and answers -->
<script src="/tests/app/arrays.js"></script>
<script src="/app/arrays.js"></script>
<script src="/tests/app/arraysExtraCredit.js"></script>
<script src="/app/arraysExtraCredit.js"></script>
<script src="/tests/app/async.js"></script>
<script src="/app/async.js"></script>
<script src="/tests/app/bestPractices.js"></script>
Expand Down