function document.write does not write two messages [closed]

3

I started a month and a half of studies and the little code below is not writing the name and the points of the two teams informed, only one. If you remove from the code the writing of the data of the first time it normally displays the result of the second. I also replace the document.write in the 'show' function with the console.log and on the console it displays the results of the two teams normally. I read in some articles an information that the document.write method erases the previous information, but I did not understand how this works. If anyone can help me thank you.

    <script>

        function pulaLinha () {
            document.write ("<br");
        }

        function mostra (frase) {
            document.write (frase);
            pulaLinha ();
        }

        function perguntaJogosECalcRes (nomTime) {
            var vit = prompt ("Quantos jogos o time " + nomTime + " venceu?");
            var emp = prompt ("Quantos jogos o time " + nomTime + " empatou?");
            var pontos = (vit * 3) + parseInt (emp, 10);
            return pontos;
        }

        var nomTime1 = prompt ("Qual o nome do primeiro time?");
        var nomTime2 = prompt ("Qual o nome do segundo time?");

        var ponTime1 = perguntaJogosECalcRes (nomTime1);
        var ponTime2 = perguntaJogosECalcRes (nomTime2);

        mostra (nomTime1 + " " + ponTime1);
        mostra (nomTime2 + " " + ponTime2);

    </script>
    
asked by anonymous 19.10.2015 / 21:29

1 answer

4

The document.write function is full of quirks. For example, it does different things while the document is loading and after it finishes loading.

If you want to print some things for debugging, it's best to use console.log

If you want to show something to the user, use DOM methods:

function mostra(frase)
    var span = document.createElement("div");
    var text = document.createTextNode(frase);
    span.appendChild(text);

    document.body.appendChild(span);
}
    
19.10.2015 / 21:43