Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

187
Views
How Do I Implement Operations Into A Stacked List

I am currently working on implementing an RPN calculator into a stack. The numbers push into the stack just fine, however, when trying to do operations within the stack, it seems as though there is a hiccup somewhere.

For example; when pushing 5 and 6 into the stack, the output is then 5, 6,. However upon entering + into the stack, it returns with 5, [object Object][object Object], instead of 5, 11,. How would I go about implementing future operations correctly to work within the stack and its contents?

var Node = function(_content) {
  this.next = null;
  this.last = null;
  this.content = _content;
};

var Stack = function() {
  this.top = null;
  this.bottom = null;
  this.push = function(_content) {
    if (this.bottom == null) {
      this.bottom = new Node(_content);
      this.top = this.bottom;
      return this;
    }

    var myNode = new Node(_content);
    myNode.last = this.top;
    this.top.next = myNode;
    this.top = myNode
  }

  this.pop = function() {
    if (this.bottom == null) {
      alert("The stack is empty")
      return null
    }

    if (this.bottom == this.top) {
      this.top = this.bottom
      this.top.last = null
      return this.top
    }

    var testStack = this.top
    this.top = this.top.last
    this.top.next = null
    return testStack
  }

  this.toString = function() {
    var myString = ""
    var node = this.bottom
    while (node != null) {
      myString += node.content + ", "
      node = node.next;
    }
    return myString
  }
}

function deleteScreen() {
  var n = ""
  document.getElementById("value").value = ""
}

var stack = new Stack()
var x
var y
var z
var result
var input

function push() {
  input = document.getElementById("value").value
  if (input === "" || input === " ") {
    alert("Please enter a number!")
  }
  stack.push(input)
  document.getElementById('output').innerHTML = stack.toString();
  deleteScreen()
  if (input == '+') {
    var x = stack.pop()
    var y = stack.pop()
    stack.push(x + y)
    document.getElementById('output').innerHTML = stack.toString();
  }
};
<input style="width: 100px" type="textbox" id='value' />
<input type="button" id="assign" value="Insert Number Into Stack" onclick="push()" />
<p id="output">
</p>

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!