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
| export class Queue<T> { | |
| constructor(private length: number, private data: T[] = []) {} | |
| add(record) { | |
| const length = this.data.unshift(record); | |
| if (length > this.length) { | |
| this.remove(); | |
| } | |
| } |
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
| export class Queue<T> { | |
| constructor(private length: number, private data: T[] = []) {} | |
| add(record) { | |
| const length = this.data.unshift(record); | |
| if (length > this.length) { | |
| this.remove(); | |
| } | |
| } |
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
| class Range { | |
| constructor(start, stop, step=1) { | |
| this.start = start; | |
| this.stop = stop; | |
| this.step = step; | |
| this.cursor = start - step; | |
| } | |
| [Symbol.iterator]() { | |
| let {start, stop, step, cursor}; |
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
| for (let number of new Range(1, 5)) { | |
| console.log(number); | |
| } | |
| //1 | |
| //2 | |
| //3 | |
| //4 | |
| //5 |
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 numbersArray = [...new Range(1, 10)]; | |
| // [1, 2, 3, 4, 5, 6, 7, 8, 9 ,10] |
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 [first, ...rest] = new Range(1, 10); | |
| console.log(first); // 1 | |
| console.log(rese); // [2, 3, 4, 5, 6, 7, 8, 9, 10] |
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 queue = new Queue(10); | |
| queue.add(1); | |
| queue.add(2); | |
| queue.add(3); | |
| queue.add(4); | |
| for(let item of queue) { | |
| console.log(item); |
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 numbers = [1, 2, 3, 4]; | |
| for (const number of numbers) { | |
| console.log(number) | |
| } | |
| // 1 | |
| // 2 | |
| // 3 | |
| // 4 |
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
| let petName; | |
| if(person && person.pet && person.pet.name) { | |
| petName = person.pet.name; | |
| } | |
| //Can be condensed to | |
| let petName = person?.pet?.name; |
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
| let length= 0; | |
| let name= ""; | |
| let length = number || 25; | |
| let name = name || "John"; |