Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 : 
 # # # #
# # # # 
 # # # #
# # # # 


>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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>

Will render, and without spaces before :

Hello world

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading