Created
February 8, 2017 09:48
-
-
Save breakstorm/7347fb8937d8060659a6653a03183423 to your computer and use it in GitHub Desktop.
prototype_array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyArray { | |
constructor() { | |
this.arr = []; | |
} | |
get(index) { | |
return this.arr[index]; | |
} | |
insert(index, value) { | |
this.arr.splice(index, 0, value); | |
// var length = this.arr.length; //나중에는 length()로 사용) | |
// for(i=length+1; i > index; i--){ | |
// this.arr[i] = this.arr[i-1]; | |
// } | |
// this.arr[index] = value; | |
// console.log(this.arr); | |
} | |
delete(index) { | |
this.arr.splice(index,1); | |
} | |
printAll() { | |
console.log(this.arr); | |
} | |
get length() { | |
return this.arr.length; | |
} | |
//search value and return index of the value | |
search(value) { | |
return this.arr.indexOf(value); | |
} | |
} | |
/***************** | |
* test code | |
* ignore below | |
*****************/ | |
var testcount = 0; | |
var success = 0; | |
var assert = function(condition, msg) { | |
testcount++; | |
if(!condition) { | |
console.log(msg + " test fail."); | |
} | |
else | |
success++; | |
} | |
var testResult = function() { | |
console.log(success + "/" + testcount + " test passed."); | |
} | |
var a = new MyArray(); | |
for (let i = 0; i < 10; i++) { | |
a.insert(i, i); | |
} | |
a.printAll(); | |
assert(a.length === 10, "Array size"); | |
assert(a.search(6) === 6, "Search"); | |
for (let i = 8; i >=0; i-=2) { | |
a.delete(i); | |
} | |
a.printAll(); | |
assert(a.length === 5, "Delete"); | |
assert(a.search(6) === -1, "Not found"); | |
testResult(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment