Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function createSortedList() {
- class NewList {
- constructor() {
- this.numbers = [];
- this.size = this.numbers.length;
- }
- add(element) {
- this.numbers.push(element);
- this.numbers.sort((a, b) => a - b);
- this.size = this.numbers.length;
- }
- remove(index) {
- if (index >= 0 && index < this.numbers.length) {
- this.numbers.splice(index, 1);
- this.numbers.sort((a, b) => a - b);
- this.size = this.numbers.length;
- }
- }
- get(index) {
- if (index >= 0 && index < this.numbers.length) {
- return this.numbers[index];
- }
- }
- }
- return new NewList();
- }
- let list = createSortedList();
- list.add(7);
- list.add(5);
- list.add(6);
- console.log(list.size);
- console.log(list.get(1));
- list.remove(1);
- console.log(list.get(1));
- console.log(list.size);
Advertisement
Add Comment
Please, Sign In to add comment