How to turn Document.write into column

Asked

Viewed 41 times

-1

I’m trying to make an HTML calculator in Javascript. I already made the calculator, but I’m having trouble passing the results to HTML text. I tried to put in Document.text, but it gets all stuck. That way:

10 x 0 = 010 x 1 = 1010 x 2 = 2010 x 3 = 3010 x 4 = 4010 x 5 = 5010 x 6 = 6010 x 7 = 7010 x 8 = 8010 x 9 = 9010 x 10 = 100.

When I put \n, he ignores.

Does anyone have a solution to leave the results under each other?

This is my current code:

var multiplicador = prompt("Qual tabuada você quer?")
var limite = prompt("Até que número você quer que vá?")
var count = 0
document.write("<h3>Caso queira uma outra tabuada, aperte f5 no teclado. Caso esteja em notebook, segure a tecla Fn e depois aperte f5.</h3>")
while(count <= limite){
    var resultado = multiplicador * count; 
    document.write(multiplicador + " x " + count + " = " + resultado)
    console.log(multiplicador + " x " + count + " = " + resultado)
    count ++
}

  • ... + resultado + '<br>')

  • 2

    Well, you already have an answer. Then do a search on Document.write, there are currently very few cases where it makes sense to use this.

  • 1

    Seems to be the case mark an answer as accepted. Here we don’t write "solved/finished" on the question. If you have an answer that really helped you, mark it as accepted. If you came to the solution on your own, put in the solution as an answer. So content is more organized and easier to find in the future by other people with similar problems.

1 answer

1


Just add a <br> in document.write(...) to break the line and display the results in separate lines.

See how it looks:

var multiplicador = prompt("Qual tabuada você quer?")
var limite = prompt("Até que número você quer que vá?")
var count = 0
document.write("<h3>Caso queira uma outra tabuada, aperte f5 no teclado. Caso esteja em notebook, segure a tecla Fn e depois aperte f5.</h3>")
while(count <= limite){
    var resultado = multiplicador * count; 
    document.write(multiplicador + " x " + count + " = " + resultado + "<br>") // adicionamos a quebra de linha no final com '<br>'
    count ++
}

Browser other questions tagged

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