Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function sortedList() {
- const list = (function () {
- const collection = [];
- const collectionProcessor = {
- add(element) {
- collection.push(element);
- collection.sort(ascendingOrder);
- this.size++;
- return collection;
- },
- remove(index) {
- if (isValidIndex(index)) {
- collection.splice(index, 1);
- this.size--;
- return collection;
- }
- },
- get(index) {
- if (isValidIndex(index)) {
- return collection[index];
- }
- },
- size: 0,
- };
- return collectionProcessor;
- function ascendingOrder(a, b) {
- return a - b;
- }
- function isValidIndex(index) {
- return index >= 0 && index < collection.length;
- }
- }());
- return list;
- }
Advertisement
Add Comment
Please, Sign In to add comment