Read as much as you need till you feel like you have a grasp on the concept.
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
//Node | |
class Node { | |
constructor(el){ | |
this.element = el; | |
this.next = 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
class Stack { | |
constructor(){ | |
this.length = 0; | |
this.head = 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
//Push data in the stack | |
push = (el) => { | |
//Create a new node | |
let node = new Node(el), | |
current; | |
//Add the new node at the top | |
current = this.head; | |
node.next = current; | |
this.head = node; |
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
//Pop the item from the stack | |
pop = () => { | |
let current = this.head; | |
//If there is item then remove it | |
//and make the next element as the first | |
if(current){ | |
let el = current.element; | |
current = current.next; | |
this.head = current; |
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
//Return the first element in the stack | |
peek = () => { | |
if(this.head){ | |
return this.head.element; | |
} | |
return 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
//Convert the stack to an array | |
toArray = () => { | |
let arr = []; | |
let current = this.head; | |
while(current){ | |
arr.push(current.element); | |
current = current.next; | |
} | |
return arr; |
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 Stack { | |
constructor(){ | |
this.length = 0; | |
this.head = null; | |
} | |
//Push data in the stack | |
push = (el) => { | |
//Create a new node | |
let node = new Node(el), |
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 Queue{ | |
constructor(...items){ | |
//initialize the items in queue | |
this._items = [] | |
// enqueuing the items passed to the constructor | |
this.enqueue(...items) | |
} | |
enqueue(...items){ | |
//push items into the queue |
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
# Here is how I would implement Fizzbuzz with objects | |
# The main issue with your class is what's called | |
# single responsibility principle | |
# classes should do one thing and be about | |
# instances of those classes | |
# the fizzbuzz class should be about the loop | |
# just solving fizzbuzz individually | |
class Fizzbuzz | |
attr_accessor :value |
OlderNewer