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