Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    DDH
    DDH is offline
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    9
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Javascript stack

    Hi,

    I am having troubles creating java script stack. Basically I need to store user input from text box into stack and display how the stack grows in text box at the time of storing the values, if that makes sense. Can anyone give me a hand. This is what I got so far, but instead of stacking elements one over another it adds em one below another:

    Code:
    function userInput(e){
     
     var x = document.getElementById("TextBox1");
     
     if(e.which == 13)
     
     if (document.getElementById("input").value > "") {
     
     backStack.push(document.getElementById("input").value);
     
     x.innerHTML = backStack.join('<br/>'); 
     
     document.getElementById("input").value = "";
     
        }
     }

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,618
    Thanks
    0
    Thanked 645 Times in 635 Posts
    If you want the most recently added entry at the top instead of at the bottom then replace push() with unshift()
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    DDH (01-20-2013)

  • #3
    DDH
    DDH is offline
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    9
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Thanks! That worked, but I need to do it with LIFO so I think I must use push() and pop() instead of shift() and unshift() . Thank you again for the repsonse

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,618
    Thanks
    0
    Thanked 645 Times in 635 Posts
    LIFO can use either push() and pop() or unshift() and shift() - which you use only affects which end of the array is which

    FIFO can use either push() and shift() or unshift() and pop()
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    DDH (01-20-2013)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •