I should point out that if you use Array.push() and Array.pop() then indeed the array *acts* like a stack. It just isn't in the order you would expect it if you are used to conventional CPU stacks.
One "trick" you could pull:
Code:
function userInput(e)
{
var inp = document.getElementById("input");
if(e.which == 13 && inp.value != "" )
{
backStack.push( inp.value );
var temp = [];
temp.concat(backStack).reverse();
document.getElementById("TextBox1").innerHTML = temp.join("<br/>");
}
}
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Thanks! I've tried the "trick" you suggested, but it doesn't seems to work. I will continue to try. Thank you
I'm not sure what kink of problem you are having without seeing your code,
but this works and can be modified to show both reversed and non-reverse display.
Plus I added two new stack commands to demonstrate.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title> Untitled </title>
<script type="text/javascript">
var backStack = [];
</script>
<style type="text/css">
#TextBox1 { height:400px; width:200px;border:1px solid red; }
</style>
</head>
<body>
<input type="text" value="" id="input" onkeyup="userInput(event)">
<br>Stack Display:<br>
<div id="TextBox1"></div>
<button onclick="dropEntry()">Drop</button>
<button onclick="swapEntry()">Swap</button>
<script type="text/javascript">
function showStack() { // display in LIFO order
var x = document.getElementById("TextBox1");
var s = [].concat(backStack);
x.innerHTML = s.reverse().join('<br/>'); // .reverse() demo
// x.innerHTML = s.join('<br/>'); // no .reverse() demo
document.getElementById('input').focus();
}
function userInput(e){
if (e.which == 13) {
var info = document.getElementById('input');
if (info.value > "") {
backStack.push(info.value);
info.value = "";
showStack();
}
}
}
function dropEntry() {
if (backStack.length > 0) { backStack.pop(); } showStack();
}
function swapEntry() {
if (backStack.length > 1) {
var top0 = backStack.pop(); var top1 = backStack.pop();
backStack.push(top0); backStack.push(top1);
}
showStack();
}
</script>
</body>
</html>
Sorry, this is the code. Basically I want to display the items as it pops it , and pushes it from one stack to another.
Code:
if (backStack.length > 0) { document.getElementById('display').value = forwardStack.push(backStack.pop()); showBackStack(); showForwardStack();}
Show the WHOLE code. JS and HTML (and any CSS if used)
Where is the forwardStack coming from???
Don't make us guess at what your problem is without providing the source.