views:

126

answers:

3

This is the code I wrote up to display a Mancala board but it won't display the leading space for some reason. Does anyone know why this is happening? Any help is greatly appreciated.

function display(board)
        {
            var space = " ";
            document.write(space);
            for (var i=0;i<board.length/2;i=i+1)
            {
                document.writeln(board[i]);
            }
            document.write("<BR>");
            for (var i=board.length-1;i >= board.length/2;i=i-1)
            {
                document.writeln(board[i]);
            }
        }

edit: For whatever reason the code doesn't seem to display properly but the important part is the document.write(space) command.

A: 

Replace your " " with "&nbsp;" and see if that works.

BoltBait
+3  A: 

In HTML, whitespace is automatically trimmed down before text is rendered, so use &nbsp; instead.

Dominic Barnes
A: 

How about...

<pre>
<script>
function display(board)
    {
        var space = " ";
        document.write(space);
        for (var i=0;i<board.length/2;i=i+1)
        {
            document.writeln(board[i]);
        }
        for (var i=board.length-1;i >= board.length/2;i=i-1)
        {
            document.writeln(board[i]);
        }
    }
</script>
</pre>
Ms2ger