Skip to content
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
91 changes: 82 additions & 9 deletions ArrayUtils.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,94 @@
class ArrayUtils{

isEmpty(array){return false;}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works in JS but won't work in other languages, just a heads up

isEmpty(array){
if(array[0]) {
return false;
} else {
return true;
}
}

append(original, value){return original;}
append(original, value){
original.push(value);
return original;
}

clone(original){return original;}
clone(original){
let copy = original;
return copy;
}

subArray(original, from, to){return original;}
subArray(original, from, to){
let newArray = [];
for(let i = from; i <= to; i++) {
newArray.push(original[i]);
}
return newArray;
}

equals(arr1, arr2){return false;}
equals(arr1, arr2){
let mismatch = 0;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the arrays have different lengths than they can't be equal (-3)

if(arr1.length > arr2.length) {
for(let i = 0; i < arr1.length; i++) {
if(arr1[i] !== arr2[i]) {
mismatch ++;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is found, you could return false instead of breaking, achieves the same goal

break;
}
}
} else {
for(let i = 0; i < arr2.length; i++) {
if(arr1[i] !== arr2[i]) {
mismatch ++;
break;
}
}
}
if(mismatch === 0){
return true;
} else {
return false;
}
}

fill(original, value){}
fill(original, value){
for(let i = 0; i < original.length; i++) {
original[i] = value;
}
return original;
}

indexOf(original, value){return -1;}
indexOf(original, value){
let match;
for(let i = 0; i < original.length; i++) {
if(original[i] === value) {
match = i;
break;
}
}
if(match != null) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could start match at -1 and return the final value of match instead of checking if match is null

return match;
} else {
return -1;
}
}

remove(original, value){return original;}
remove(original, value){
let copy = original;
for(let i = 0; i < copy.length; i++) {
if(copy[i] === value) {
copy.splice(i, 1);
break;
}
}
return copy;
}

reverse(original){}
reverse(original){
let opposite = [];
for(let i = 0; i < original.length; i++) {
opposite.unshift(original[i]);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice usage of built in functions 👍

}
return opposite;
}

}