Stacking and stacking with queue in javascript

2

I have to stack 3 values that are entered by the user through the prompt, and then unpack, showing the result on the screen.

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

  

undefined

How can I fix this error and stack and unstack correctly? Thanks in advance for the 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>
    
asked by anonymous 08.10.2017 / 02:13

1 answer

4

It seems that your text also has problems, stack is CELL , and queuing is FILA , has a meaning in the world of different computer where PILHA the first one that comes in is last that exits, because the removal of items is always top and FILA the first one that comes in is the first one that comes out , but, let's go to the code that apparently was correct, but at the time it was setar the numbers did the wrong code where: p>

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

Should be this:

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

Because your Enfileira is a método that receives a value, unlike a property that can be assigned as done in the question.

Minimum 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>");
    
08.10.2017 / 05:24