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)
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
@Monteiro added a variation to insert all in the same
prompt
. About what you’re missing you can instead ofvar 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 viaprompt
. Makes sense? :)– Sergio
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 :)
– Monteiro
@Monteiro developed a bit more. But there were no gross mistakes :)
– Sergio