Stacking and popping with javascript queue

Asked

Viewed 709 times

2

I have to stack 3 values that are typed by the user through the prompt, and after this pop up, showing the result on the screen.

The problem is that after the user enters the 3 values, it appears the following word 3 times, instead of the values :

Undefined

How can I fix this error and stack and pop correctly ? I thank you for your answers.

My code :

<html>

<head>
<script type="text/javascript"/>

function FIFO(){
    this.fila = new Array();
this.Enfileira = function(obj){
    this.fila[this.fila.length] = obj;
}

this.Desenfileira = function(){
if (this.fila.length > 0){
    var obj = this.fila[0];
    this.fila.splice(0,1);
    return obj;
}else{
    alert ("Não há objetos na fila.")
}
}
}
</script>
</head>

<body>

<h1>EXEMPLO FILA</h1>
<script type="text/javascript"/>

var minhafila = new FIFO();

minhafila.Enfileira = prompt ("Digite um texto : ");
minhafila.Enfileira = prompt ("Digite um texto : ");
minhafila.Enfileira = prompt ("Digite um texto : ");

var desenf1 = minhafila.Desenfileira();
document.write(desenf1,"</br>");
var desenf2 = minhafila.Desenfileira();
document.write(desenf2,"</br>");
var desenf3 = minhafila.Desenfileira();
document.write(desenf3,"</br>");
</script>


</body>


</html>
  • What do you think minhafila.Enfileira = prompt ("Digite um texto : "); ago?

1 answer

4


It seems that your text also has problems, stack is PILE, and line up is QUEUE, has a different meaning in the computer world, where PILE the first that enters is the last that leaves, because the removal of the items is always from the top and QUEUE the first one in is the first one out, but, let’s go to the code that apparently was correct, but at the time that was set the numbers made the wrong code where:

minhafila.Enfileira = prompt ("Digite um texto : "); // errado

that must be it:

minhafila.Enfileira(prompt ("Digite um texto : ")); // correto

because his Enfileira is a método that receives a value, other than a property that can be assigned as asked in the question.

Minimal example:

function FIFO() {
  this.fila = new Array();
  this.Enfileira = function(obj) {
    this.fila[this.fila.length] = obj;
  }
  this.Desenfileira = function() {
    if (this.fila.length > 0) {
      var obj = this.fila[0];
      this.fila.splice(0, 1);
      return obj;
    } else {
      alert("Não há objetos na fila.")
    }
  }
}

var minhafila = new FIFO(); 

minhafila.Enfileira(prompt ("Digite um texto : "));
minhafila.Enfileira(prompt ("Digite um texto : "));
minhafila.Enfileira(prompt ("Digite um texto : "));

var desenf1 = minhafila.Desenfileira();
document.write(desenf1,"</br>"); 
var desenf2 = minhafila.Desenfileira();
document.write(desenf2,"</br>"); 
var desenf3 = minhafila.Desenfileira();
document.write(desenf3,"</br>");

  • 1

    Thanks, I put the terms stacking and unpacking in the wrong way, and I’m also new to javascript, so the simple mistakes. Finally, thank you very much, +1 and right answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.