Last active
September 21, 2018 16:23
-
-
Save v1b3m/d5e0cb2f19db9b6dacdbfdd40309f0f2 to your computer and use it in GitHub Desktop.
This file contains 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.stack = []; | |
} | |
push(element) { | |
this.stack.push(element); | |
} | |
pop() { | |
this.stack.pop(); | |
} | |
topMost() { | |
return this.stack[-1]; | |
} | |
length() { | |
return this.stack.length; | |
} | |
allElements() { | |
return this.stack; | |
} | |
} | |
$(document).ready(function () { | |
var arr = []; | |
$('#createStack').click(function (event) { | |
event.preventDefault(); | |
if (arr.length > 0) { | |
arr.splice(0, arr.length); | |
$('#top').empty(); | |
$('#len').empty(); | |
$('#all').empty(); | |
} | |
var stack = '[]'; | |
$('#display').empty(); | |
$('#display').append(stack); | |
}); | |
$('#addBtn').click(function (event) { | |
event.preventDefault(); | |
var element = document.getElementById('element').value; | |
if (element === '' || /^ *$/.test(element)) { | |
return; | |
} else{ | |
arr.push(element); | |
} | |
$('#display').empty(); | |
$('#top').empty(); | |
$('#len').empty(); | |
$('#all').empty(); | |
$('#display').append('[' + arr + ']'); | |
}); | |
$('#popStack').click(function (event) { | |
event.preventDefault(); | |
arr.pop(); | |
$('#display').empty(); | |
$('#top').empty(); | |
$('#len').empty(); | |
$('#all').empty(); | |
$('#display').append('[' + arr + ']'); | |
}); | |
$('#topMost').click(function (event) { | |
event.preventDefault(); | |
var top = arr[(arr.length) - 1]; | |
$('#top').empty(); | |
$('#top').append(top); | |
}); | |
$('#arrLength').click(function (event) { | |
event.preventDefault(); | |
var len = arr.length; | |
$('#len').empty(); | |
if (len === 1){ | |
$('#len').append(len + ' Element'); | |
} else { | |
$('#len').append(len + ' Elements'); | |
} | |
}); | |
$('#allElements').click(function (event) { | |
event.preventDefault(); | |
$('#all').empty(); | |
$('#all').append('[' + arr + ']'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment