This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function PriorityQueue() { | |
| let priority = [] | |
| let nonPriority = [] | |
| class PublicPriorityQueue { | |
| size() { | |
| return priority.length + nonPriority.length | |
| } | |
| isEmpty() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Stack() { | |
| let items = [] | |
| class PublicStack { | |
| peek() { | |
| if (this.isEmpty()) throw new Error('Stack is empty') | |
| const lastItemIndex = items.length - 1 | |
| return items[lastItemIndex] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Stack() { | |
| let items = [] | |
| class PublicStack { | |
| peek() { | |
| if (this.isEmpty()) throw new Error('Stack is empty') | |
| const lastItemIndex = items.length - 1 | |
| return items[lastItemIndex] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Stack() { | |
| let items = [] | |
| class PublicStack { | |
| peek() { | |
| if (this.isEmpty()) throw new Error('Stack is empty') | |
| const lastItemIndex = items.length - 1 | |
| return items[lastItemIndex] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Stack() { | |
| let top = null | |
| let size = 0 | |
| class Node { | |
| constructor(data) { | |
| this.data = data | |
| this.previous = null | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Stack() { | |
| let items = [] | |
| class PublicStack { | |
| peek() { | |
| if (this.isEmpty()) throw new Error('Stack is empty') | |
| const lastItemIndex = items.length - 1 | |
| return items[lastItemIndex] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const animals = ['elephant', 'monkey', 'snake', 'lion', 'zebra'] | |
| const reverseArray = currentArray => { | |
| for (let index = 0; index < currentArray.length / 2; index++) { | |
| const currentItem = currentArray[index] | |
| const otherItem = currentArray[currentArray.length - 1 - index] | |
| currentArray[index] = otherItem | |
| currentArray[currentArray.length - 1 - index] = currentItem | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const animals = ['elephant', 'monkey', 'snake', 'lion'] | |
| const desiredPositionToRemove = 2 | |
| for (var index = desiredPositionToRemove; index < animals.length; index++) { | |
| animals[index] = animals[index + 1] | |
| } | |
| animals.length = animals.length - 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const animals = ["elephant", "monkey", "snake", "lion"]; | |
| for (let index = 0; index < animals.length; index++) { | |
| animals[index] = animals[index + 1]; | |
| } | |
| animals.length = animals.length - 1; | |
| console.log(animals); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const animals = ["elephant", "monkey", "snake", "lion"]; | |
| const positionToAddNewItem = 2; | |
| for (let index = animals.length; index > positionToAddNewItem; index--) { | |
| animals[index] = animals[index - 1]; | |
| } | |
| animals[positionToAddNewItem] = "macaw"; |