How to insert blank spaces - Javascript

3

I'm doing some exercises and one of them asks me to create a code that reproduces an "ASCII art", I read about it and saw it was a special character and tried to insert space in its unicode form "\ s", "\ u0020 ",but without success. I saw some people talking about using regular expressions. Thanks in advance

<script>
document.write("XXXXX<br>");
for (var i =0;i<2;i++){
document.write("X   X<br>")
};
document.write("XXXXX<br>");
</script>


                      XXXXX
                      X   X
                      X   X   
                      XXXXX
    
asked by anonymous 06.08.2018 / 16:55

1 answer

5

The space problem, even using the &nbsp; HTML entity, is the rendering of the characters. In proportional fonts, each character occupies a different space according to its width (width). In fonts called monospaced (single space) any character occupies the even space, such as the Consolas font, which is widely used in programming and is also the default font of this response editor, as shown below:

Seethatallthefontslistedabovearemonospaced.

Consolesourcesample:

Soyouhavetwooptions:

1.Usingthe<pre>tag

The<pre>tagshowsthetextasitis,alreadyconvertingthefonttypetomonospace:

<pre>
<script>
document.write("XXXXX<br>");
for (var i =0;i<2;i++){
document.write("X   X<br>")
};
document.write("XXXXX<br>");
</script>
</pre>

2. Use entity &nbsp; :

In this case you need to use a monospace font:

body{
   font-family: consolas;
}
<script>
document.write("XXXXX<br>");
for (var i =0;i<2;i++){
document.write("X&nbsp;&nbsp;&nbsp;X<br>")
};
document.write("XXXXX<br>");
</script>
    
06.08.2018 / 17:57