-
Notifications
You must be signed in to change notification settings - Fork 5
Completed Lab #1
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,94 @@ | ||
| class ArrayUtils{ | ||
|
|
||
| isEmpty(array){return false;} | ||
| 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; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ++; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is found, you could return |
||
| 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) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| 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]); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice usage of built in functions 👍 |
||
| } | ||
| return opposite; | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
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