Created
February 28, 2018 03:17
-
-
Save setkyar/6d6ddd706e5cc0802898ad1bbaa4560d to your computer and use it in GitHub Desktop.
Stacks (Solution)
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
var Stack = function(){ | |
this.storage = {}; | |
this.count = 0; | |
}; | |
Stack.prototype.push = function(val) { | |
this.storage[this.count++] = val; | |
return this.count; | |
} | |
Stack.prototype.pop = function() { | |
var value = this.storage[--this.count]; | |
delete this.storage[this.count]; | |
if (this.count < 0) { | |
this.count = 0; | |
} | |
return value; | |
} | |
Stack.prototype.size = function() { | |
return this.count; | |
} | |
var myWeeklyMenu = new Stack(); | |
myWeeklyMenu.push("Coffee"); | |
myWeeklyMenu.push("Rice"); | |
myWeeklyMenu.push("Cold Drink"); | |
myWeeklyMenu.pop(); | |
myWeeklyMenu.size(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please feel free to ask me about my answer gist. :)