Skip to content
This repository has been archived by the owner on Nov 7, 2020. It is now read-only.

Added the solutions to the arrays extra credit tasks #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
54 changes: 54 additions & 0 deletions app/arraysExtraCredit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] === item) {
return i;
}
}
return -1;
},

push : function(arr,item) {
arr[arr.length] = item;
return arr.length;
},

pop : function(arr) {
var temp = arr[arr.length-1];
arr.length--;
return temp;
},

unshift : function(arr,item) {
for (var i = arr.length-1; i >= 0; i--) {
arr[i+1] = arr[i];
}
arr[0] = item;
return arr.length;
},

shift : function(arr) {
var temp = arr[0];
for (var i = 0; i < arr.length; i++) {
arr[i] = arr[i+1];
}
arr.length--;
return temp;
},

concat : function(arr1, arr2) {
var newArray = [];
for (var i = 0; i < arr1.length; i++) {
newArray.push(arr1[i]);
}
for (var i = 0; i < arr2.length; i++) {
newArray.push(arr2[i]);
}
return newArray;
}
}