Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

102
Visualizações
Chessboard for beginner

I am trying to print Chessboard. Why is this not working? This code is working for console.log. I want to print it by document.write() Thanx


let size = 8
let result = ''

for(let i = 0 ; i < size ; i++) {
  for(let j = 0; j < size ; j++) {

    if(( i + j ) % 2  === 0) {
      result += ' '
    } else {
      result += '#'
    }
  }
  result += '<br>'
} 
document.write(result)

// my output is :
# # # #
# # # #
# # # #
# # # #


//it should like this : 
 # # # #
# # # # 
 # # # #
# # # # 


about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

To keep constistancy between well indented HTML code and rendered text, browsers trim lines, not showing leading and trailing whitespaces.

In example :

<div>
    Hello world
</div>

You can notice in the snippet, "Hello world" is indented within the <div></div>. When you click "Run code snippet", the rendered HTML is left aligned as if there was no indentation.

If you want to add "visible" spaces, you can use the non-breaking space character code, &nbsp

let size = 8
let result = ''

for(let i = 0 ; i < size ; i++) {
  for(let j = 0; j < size ; j++) {

    if(( i + j ) % 2  === 0) {
      result += '&nbsp;' // <--------------- Check this
    } else {
      result += '#'
    }
  }
  result += '<br>'
} 
document.write(result)
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda