1
I am trying to do an exercise where I have to take 5 names in 5 different input, store them in an array and print them in the same fields but in reverse order to what they were.
function inverteNomes(){
var nome1 = document.getElementById("txtNome0").value;
var nome2 = document.getElementById("txtNome1").value;
var nome3 = document.getElementById("txtNome2").value;
var nome4 = document.getElementById("txtNome3").value;
var nome5 = document.getElementById("txtNome4").value;
var arrayNomes = [nome1, nome2, nome3, nome4, nome5];
var invertido = arrayNomes.reverse();
for(var i = 0; i < 5; i++){
var nome = document.getElementById("txtNome" + i);
nome.innerHTML = invertido[i];
}
}
Rafael’s answer already points out the problem, here is a suggestion to be more DRY: http://jsfiddle.net/hkco5c00/
– Sergio