Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

189
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda