Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-20-2013, 06:13 PM   PM User | #1
DDH
New to the CF scene

 
Join Date: Jan 2013
Posts: 9
Thanks: 10
Thanked 0 Times in 0 Posts
DDH is an unknown quantity at this point
Need help with JS stack

Need a bit f help wit JS

Last edited by DDH; 01-21-2013 at 05:47 PM..
DDH is offline   Reply With Quote
Old 01-20-2013, 09:56 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,056 Times in 4,025 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Array.push() is *NOT* a push-down stack.

It does exactly what you are seeing.

Basically if you do this:
Code:
var ary = [ ];
ary.push("apple");
ary.push("banana");
ary.push("coleslaw");
you get exactly the same result as if you had done
Code:
var ary = [ ];
ary[0] = "apple";
ary[1] = "banana";
ary[2] = "coleslaw";
which is the same you would get if you did
Code:
var ary = ["apple","banana","coleslaw"];
You can argue that the push( ) method of Array is misnamed. But it's way too late to change it now. It is what it is.
__________________
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.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
DDH (01-20-2013)
Old 01-20-2013, 10:05 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,056 Times in 4,025 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
DDH (01-20-2013)
Old 01-20-2013, 11:13 PM   PM User | #4
DDH
New to the CF scene

 
Join Date: Jan 2013
Posts: 9
Thanks: 10
Thanked 0 Times in 0 Posts
DDH is an unknown quantity at this point
Thanks! I've tried the "trick" you suggested, but it doesn't seems to work. I will continue to try. Thank you
DDH is offline   Reply With Quote
Old 01-21-2013, 01:15 AM   PM User | #5
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,797
Thanks: 30
Thanked 462 Times in 456 Posts
jmrker will become famous soon enough
Lightbulb

Quote:
Originally Posted by DDH View Post
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>
jmrker is offline   Reply With Quote
Users who have thanked jmrker for this post:
DDH (01-21-2013)
Old 01-21-2013, 04:41 AM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,056 Times in 4,025 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Actually, you don't even need the intermediate temporary variable (temp in my code, s in jmrker's code).

This works:
Code:
<!DOCTYPE html>
<html>
<body>
<body>
<form onsubmit="return false;">
<input type="text" value="" id="input" />
</form>
<br>Stack Display:<br>
<div id="TextBox1"></div>

<script type="text/javascript">
(
  function()
  {
      var backStack = [ ];
      var inp = document.getElementById("input");
      inp.onkeyup = userInput;    

      function userInput(e)
      {
          if(e.which == 13 && inp.value != "" )
          { 
              backStack.push( inp.value );
              document.getElementById("TextBox1").innerHTML = 
                     [].concat(backStack).reverse().join("<br/>");
              inp.value = "";
              inp.focus();
         }
      }
  }
)();
</script>
</body>
</html>
__________________
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.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
DDH (01-21-2013)
Old 01-21-2013, 12:29 PM   PM User | #7
DDH
New to the CF scene

 
Join Date: Jan 2013
Posts: 9
Thanks: 10
Thanked 0 Times in 0 Posts
DDH is an unknown quantity at this point
Thank you!
DDH is offline   Reply With Quote
Old 01-21-2013, 01:19 PM   PM User | #8
Airblader
Regular Coder

 
Join Date: Jan 2013
Location: Germany
Posts: 374
Thanks: 3
Thanked 44 Times in 44 Posts
Airblader can only hope to improve
You could also use shift/unshift instead of push/pop.
Airblader is offline   Reply With Quote
Users who have thanked Airblader for this post:
DDH (01-21-2013)
Old 01-21-2013, 01:46 PM   PM User | #9
DDH
New to the CF scene

 
Join Date: Jan 2013
Posts: 9
Thanks: 10
Thanked 0 Times in 0 Posts
DDH is an unknown quantity at this point
Is there a way to display the popped element from an array into a text box.
DDH is offline   Reply With Quote
Old 01-21-2013, 02:30 PM   PM User | #10
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,797
Thanks: 30
Thanked 462 Times in 456 Posts
jmrker will become famous soon enough
Question

Quote:
Originally Posted by DDH View Post
Is there a way to display the popped element from an array into a text box.

Which text box? The entry box or a different one?

Assuming input box and code from post #5:
Code:
function dropEntry() {
  if (backStack.length > 0) { var top0 = backStack.pop(); document.getElementById('TextBox1').value = top0; }
  showStack();
}
You should try some of the modifications yourself to see how it works for future reference.

Last edited by jmrker; 01-21-2013 at 02:35 PM..
jmrker is offline   Reply With Quote
Users who have thanked jmrker for this post:
DDH (01-21-2013)
Old 01-21-2013, 03:39 PM   PM User | #11
DDH
New to the CF scene

 
Join Date: Jan 2013
Posts: 9
Thanks: 10
Thanked 0 Times in 0 Posts
DDH is an unknown quantity at this point
Now I have a displayed item, but instead of the letters it displays 1,2,3....
DDH is offline   Reply With Quote
Old 01-21-2013, 04:47 PM   PM User | #12
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,797
Thanks: 30
Thanked 462 Times in 456 Posts
jmrker will become famous soon enough
Exclamation

Quote:
Originally Posted by DDH View Post
Now I have a displayed item, but instead of the letters it displays 1,2,3....
That doesn't make sense.
Show the code you are testing.
jmrker is offline   Reply With Quote
Users who have thanked jmrker for this post:
DDH (01-21-2013)
Old 01-21-2013, 04:52 PM   PM User | #13
DDH
New to the CF scene

 
Join Date: Jan 2013
Posts: 9
Thanks: 10
Thanked 0 Times in 0 Posts
DDH is an unknown quantity at this point
Basically I want to display the items as it pops it , and pushes it from one stack to another.

Last edited by DDH; 01-21-2013 at 05:52 PM..
DDH is offline   Reply With Quote
Old 01-21-2013, 04:57 PM   PM User | #14
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,797
Thanks: 30
Thanked 462 Times in 456 Posts
jmrker will become famous soon enough
Exclamation

Quote:
Originally Posted by DDH View Post
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.
jmrker is offline   Reply With Quote
Users who have thanked jmrker for this post:
DDH (01-21-2013)
Old 01-21-2013, 04:59 PM   PM User | #15
DDH
New to the CF scene

 
Join Date: Jan 2013
Posts: 9
Thanks: 10
Thanked 0 Times in 0 Posts
DDH is an unknown quantity at this point
Okay.

Last edited by DDH; 01-21-2013 at 05:13 PM..
DDH is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:33 PM.


Advertisement
Log in to turn off these ads.