Skip to content

Instantly share code, notes, and snippets.

@Nikola-Andreev
Created November 2, 2016 22:33
Show Gist options
  • Save Nikola-Andreev/47b7fce9d4ea633e87d6f43eaa02dd39 to your computer and use it in GitHub Desktop.
Save Nikola-Andreev/47b7fce9d4ea633e87d6f43eaa02dd39 to your computer and use it in GitHub Desktop.
Classes Exercises
class Class {
constructor() {
this.arr = []
this.size = 0
}
add(element) {
this.arr.push(element)
this.size += 1
return this.arr.sort((a, b) => {
return a - b
})
}
remove(index) {
if (this.arr.length > index && index >= 0) {
this.arr.splice(index, 1)
this.size--
} else {
throw new Error
}
return this.arr.sort((a, b) => {
return a - b
})
}
get(index) {
if (this.arr.length > index && index >= 0) {
return this.arr[index]
} else {
throw new Error
}
}
}
var myList = new Class();
console.log(myList.size())
myList.add(5)
console.log(myList.get(0))
console.log(myList.size())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment