1

Hello I've looked through JavaScript related questions and can find no result. What I'm trying to do is very simple; I know how to do it but for some reason it isn't working.

Here's the code I'm having the problem with:

playersScore = rollDie();
document.write('Score: ' + playersScore);
playersPosition = playersScore + playersPosition;
document.write(', Square: ' + playersPosition);

indexOfNumber = findIndexOf(playersPosition, specialSquaresArray);

if (indexOfNumber != -1) {
    document.write(', Ladder to Square: ' + connectedSquaresArray[indexOfNumber] + '<BR>');
    playersPosition = connectedSquaresArray[indexOfNumber];
    indexOfNumber = -1;
}
// end of question(iii) 
// start of question(iv)(b) 
while (playersPosition < 80) {
    playersScore = rollDie()
    document.write('Score: ' + playersScore)
    playersPosition = playersPosition + playersScore
    document.write(', Square: ' + playersPosition)
    indexOfNumber = findIndexOf(playersPosition, specialSquaresArray)
    if (indexOfNumber != -1) {
        document.write(', Ladder to Square: ' + connectedSquaresArray[indexOfNumber]);
        playersPosition = connectedSquaresArray[indexOfNumber];
    }
    document.write('<BR>');

And here's the result in the browser:

Score: 4, Square: 4Score: 4, Square: 8
Score: 2, Square: 10
Score: 1, Square: 11
Score: 2, Square: 13
Score: 5, Square: 18
Score: 4, Square: 22
Score: 1, Square: 23
Score: 5, Square: 28
Score: 3, Square: 31
Score: 5, Square: 36
Score: 3, Square: 39, Ladder to Square: 51
Score: 4, Square: 55
Score: 6, Square: 61
Score: 6, Square: 67
Score: 1, Square: 68, Ladder to Square: 73
Score: 1, Square: 74
Score: 3, Square: 77, Ladder to Square: 58
Score: 5, Square: 63
Score: 1, Square: 64
Score: 4, Square: 68, Ladder to Square: 73
Score: 6, Square: 79
Score: 6, Square: 85

The first line is the player's 'first go' (Score: 4, Square: 4) yet I need the line that immediately follows it (Score: 4, Square: 8) to break to the line below but no matter where I place <BR> it will not do it. Is the a problem with the first few lines or is it an issue at the start of the while loop? I really can't figure it out!!

I'd really appreciate some help with this. Many thanks in advance.

1
  • Guys stop editing, one more and it becomes CW. Commented Feb 28, 2011 at 12:40

5 Answers 5

3

Just write line break if no ladder is found:

if (indexOfNumber != -1) {
    document.write(', Ladder to Square: ' + connectedSquaresArray[indexOfNumber] + '<BR>');
    playersPosition = connectedSquaresArray[indexOfNumber];
    indexOfNumber = -1;
}
else {
   document.write('<BR>');
}
Sign up to request clarification or add additional context in comments.

Comments

1

Change line number 4 to the following

document.write(', Square: ' + playersPosition + '<br/>');

1 Comment

Not good.. what if "Ladder to Square" is found? He want it in the same line.
0

Put another

     document.write('<BR>'); 

as the first line of While Loop.

Comments

0

You would have to explicitly write out HTML and line breaks, e.g. document.write('<br />');.

Comments

0

You need to add a

document.write('<BR>');

before your while loop to break the line after the first section of output. You do have a BR up there but it only gets output if the if test passes.

Remove the BR from the first 'Ladder to Square:' output line and just add one before the while loop so that it will be always be output.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.