最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How to add a space at the end of a string in Javascript - Stack Overflow

programmeradmin2浏览0评论

I am writing a program which creates a slanted d based off of user input. The program asks for the number of rows and produces the output accordingly. If the user enters in 5 it should produce an output of:

d
 d
  d
   d

My actual output is:

d
d
d
d

Here is my Javascript code:

spaceArray = []; 
space = ' '; 
spaceMain = ' ';


function processingFunction(rows){ //passes the input as variable rows
    var spaceCounter = 0;          
    var counter = 0;               

    while (counter<rows){    
        while (spaceCounter<counter){ 
            spaceMain = spaceMain + space;  //adds a space before the d
            spaceCounter++;         
        }
        counter++; 
        spaceCounter=0; 
        spaceArray.push(spaceMain);  //adds spaceMain to the end of spaceArray
        spaceMain = ' '; 
    }

    for (i = 0; i<rows; i++){
        d = spaceArray[i]+"d<br>";
        document.getElementById("outputNumber1").innerHTML += d;                                                                                                   
    }

}

When I replace the space variable string from ' ' to '-' it seems to work with an output of:

d
-d
--d
---d

I am not sure what I need to change to get the above output but with spaces instead of dashes.

HTML:

<!DOCTYPE>
<html>
<head>
</head>
<style>
    body{background-color: green;} 

</style>

<body>
    <center><h2>Slanted d</h2></center>
    <h3>Input the number of rows you would like your slanted d to be in the input text box</h3>
    <input type="text" id="inputNumber"></input> 
    <button type="button" onclick="processingFunction(document.getElementById('inputNumber').value)">Create slanted d</button> 
    <br><br>
    <output type="text" id="outputNumber1"></output>

<script src="slantedDJS.js"></script>

</body>
</html>

I am writing a program which creates a slanted d based off of user input. The program asks for the number of rows and produces the output accordingly. If the user enters in 5 it should produce an output of:

d
 d
  d
   d

My actual output is:

d
d
d
d

Here is my Javascript code:

spaceArray = []; 
space = ' '; 
spaceMain = ' ';


function processingFunction(rows){ //passes the input as variable rows
    var spaceCounter = 0;          
    var counter = 0;               

    while (counter<rows){    
        while (spaceCounter<counter){ 
            spaceMain = spaceMain + space;  //adds a space before the d
            spaceCounter++;         
        }
        counter++; 
        spaceCounter=0; 
        spaceArray.push(spaceMain);  //adds spaceMain to the end of spaceArray
        spaceMain = ' '; 
    }

    for (i = 0; i<rows; i++){
        d = spaceArray[i]+"d<br>";
        document.getElementById("outputNumber1").innerHTML += d;                                                                                                   
    }

}

When I replace the space variable string from ' ' to '-' it seems to work with an output of:

d
-d
--d
---d

I am not sure what I need to change to get the above output but with spaces instead of dashes.

HTML:

<!DOCTYPE>
<html>
<head>
</head>
<style>
    body{background-color: green;} 

</style>

<body>
    <center><h2>Slanted d</h2></center>
    <h3>Input the number of rows you would like your slanted d to be in the input text box</h3>
    <input type="text" id="inputNumber"></input> 
    <button type="button" onclick="processingFunction(document.getElementById('inputNumber').value)">Create slanted d</button> 
    <br><br>
    <output type="text" id="outputNumber1"></output>

<script src="slantedDJS.js"></script>

</body>
</html>
Share Improve this question asked Mar 4, 2017 at 17:32 Tomasz DobrowolskiTomasz Dobrowolski 771 gold badge1 silver badge5 bronze badges 2
  • That's a really good question on which many are stuck. an up-vote for that. – Shakti Phartiyal Commented Mar 4, 2017 at 17:37
  • Don't exactly know your reasoning for this task but to me it seems more elegant if you create an element per line and position them by CSS properties like position, left, margin, padding, etc. – Redu Commented Mar 4, 2017 at 17:52
Add a comment  | 

4 Answers 4

Reset to default 8

In place of spaces use the html code &nbsp; your code will start working, because by default the browser accepts only a single space no matter how many you give. Example:

space = '&nbsp;'; 

By default, the browser will collapse all spaces into a single space.

One solution is to use the <pre> element for the output instead. Like this:

<pre type="text" id="outputNumber1"></pre>

<pre> shows pre-formatted text. So the output is displayed with all spaces and newlines preserved.

If you don't want to change the element type, you can add the CSS declaration white-space:pre to the element to achieve the same result as above. Like this:

<output type="text" id="outputNumber1" style="white-space:pre;"></output>

or you can set it separately in your <style> block like this:

<style>
    body{background-color: green;} 
    #outputNumber1{white-space:pre;}
</style>

You can use &nbsp which stands for No-break space In the html language.

use nbsp this add a non breaking space to your html.

发布评论

评论列表(0)

  1. 暂无评论