How to row and row with separate rows in Javascript?

Asked

Viewed 131 times

1

I have two rows, and each of these rows will be filled by 3 numbers. The user will place the numbers through the prompt, that will appear on the screen only after entering the data. Even this part I’m getting, but the problem is I need a third row that will be the sum of the two previous rows. How can I do this ?

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 minhafila1 = new FIFO(); 

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

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

        var minhafila2 = new FIFO();

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

        var desenf4 = minhafila2.Desenfileira();
        document.write(desenf4,"</br>"); 
        var desenf5 = minhafila2.Desenfileira();
        document.write(desenf5,"</br>"); 
        var desenf6 = minhafila2.Desenfileira();
        document.write(desenf6,"</br>"); 

<!-- aqui ficaria o código da terceira fila, sendo a soma das duas filas anteriores -- >

    </script>


</body>


</html>

Example of how it would look :

1,2,3  < -- Primeira fila

1,2,3  < -- Segunda fila

2,4,6  < -- Terceira fila sendo a soma das duas filas anteriores, cada parte da fileira sendo uma soma da outra (1+1, 2+2, 3+3)

1 answer

1


function FIFO(max) {
  this.comprimentoFilas = max || 3;
  this.filas = [];
  this.adicionar = function(arr) {
    if (!arr) {
      arr = [];
      for (var i = 0; i < this.comprimentoFilas; i++) {
        arr.push(prompt("Digite um texto : "));
      }
    }
    arr = arr.map(Number);
    this.filas.push(arr);
    document.write(String(arr), "</br>"); 
    return arr;
  }

  this.remover = function() {
    if (this.filas.length > 0) {
      return this.filas.unshift();
    } else {
      alert("Não há objetos na fila.");
    }
  }
  this.somar = function() {
    var soma = [];
    for (var i = 0; i < this.comprimentoFilas; i++) {
      var somaColuna = 0;
      for (var j = 0; j < this.filas.length; j++) {
        somaColuna += this.filas[j][i];
      }
      soma[i] = somaColuna;
    }
    document.write(String(soma), "</br>"); 
  }
}

var filas = new FIFO();
var minhaFila1 = filas.adicionar();
var minhaFila2 = filas.adicionar([2, 3, 4]);
filas.somar();

If you want to insert the three of a kind it could be like this:

function FIFO(max) {
  this.comprimentoFilas = max || 3;
  this.filas = [];
  this.adicionar = function(arr) {
    if (!arr) {
      var arr = prompt("Digite um " + this.comprimentoFilas + " numero" + (this.comprimentoFilas > 1 ? "s:" : ":")).split(/[^\d]/g).filter(Boolean);
    }
    arr = arr.map(Number);
    this.filas.push(arr);
    document.write(String(arr), "</br>");
    return arr;
  }

  this.remover = function() {
    if (this.filas.length > 0) {
      return this.filas.unshift();
    } else {
      alert("Não há objetos na fila.");
    }
  }
  this.somar = function() {
    var soma = [];
    for (var i = 0; i < this.comprimentoFilas; i++) {
      var somaColuna = 0;
      for (var j = 0; j < this.filas.length; j++) {
        somaColuna += this.filas[j][i];
      }
      soma[i] = somaColuna;
    }
    document.write(String(soma), "</br>");
  }
}

var filas = new FIFO();
var minhaFila1 = filas.adicionar();
var minhaFila2 = filas.adicionar([2, 3, 4]);
filas.somar();

  • Thank you very much for the answer, I would only need the user to enter the values of one in one, in the two rows. Otherwise, this perfect :)

  • @Monteiro added a variation to insert all in the same prompt. About what you’re missing you can instead of var minhaFila2 = filas.adicionar([2, 3, 4]); call this one .adicionar() with nothing, to add new line(s). If you use with an array .adicionar([2, 3, 4]); he uses it instead of asking via prompt. Makes sense? :)

  • 1

    Thank you very much, it makes sense. I’m new in Javascript so I’m sorry for the grotesque errors, anyway, the answer was perfect :)

  • 1

    @Monteiro developed a bit more. But there were no gross mistakes :)

Browser other questions tagged

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