Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e1613b0

Browse files
Merge pull request #2 from hemanth/master
Add data structures in es6
2 parents ac9f0ba + 36c7362 commit e1613b0

File tree

11 files changed

+1454
-0
lines changed

11 files changed

+1454
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class MyArray {
2+
constructor() {
3+
this.array = [];
4+
}
5+
6+
add(data) {
7+
this.array.push(data);
8+
}
9+
10+
remove(data) {
11+
this.array = this.array.filter(current => current !== data);
12+
}
13+
14+
search(data) {
15+
const foundIndex = this.array.indexOf(data);
16+
if(~foundIndex) {
17+
return foundIndex;
18+
}
19+
20+
return null;
21+
}
22+
23+
getAtIndex(index) {
24+
return this.array[index];
25+
}
26+
27+
length() {
28+
return this.array.length;
29+
}
30+
31+
print() {
32+
console.log(this.array.join(' '));
33+
}
34+
}
35+
36+
const array = new MyArray();
37+
array.add(1);
38+
array.add(2);
39+
array.add(3);
40+
array.add(4);
41+
array.print(); // => 1 2 3 4
42+
console.log('search 3 gives index 2:', array.search(3)); // => 2
43+
console.log('getAtIndex 2 gives 3:', array.getAtIndex(2)); // => 3
44+
console.log('length is 4:', array.length()); // => 4
45+
array.remove(3);
46+
array.print(); // => 1 2 4
47+
array.add(5);
48+
array.add(5);
49+
array.print(); // => 1 2 4 5 5
50+
array.remove(5);
51+
array.print(); // => 1 2 4

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /