Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, March 11, 2017

Problems with a single line break in JavaScript

Problems with a single line break in JavaScript


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] + '
'); 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('
');

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
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.

Answer by Arun for Problems with a single line break in JavaScript


Change line number 4 to the following

document.write(', Square: ' + playersPosition + '
');

Answer by Shadow Wizard for Problems with a single line break in JavaScript


Just write line break if no ladder is found:

if (indexOfNumber != -1) {      document.write(', Ladder to Square: ' + connectedSquaresArray[indexOfNumber] + '
'); playersPosition = connectedSquaresArray[indexOfNumber]; indexOfNumber = -1; } else { document.write('
'); }

Answer by Furqan for Problems with a single line break in JavaScript


Put another

     document.write('
');

as the first line of While Loop.

Answer by pcjuzer for Problems with a single line break in JavaScript


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

Answer by Steve Claridge for Problems with a single line break in JavaScript


You need to add a

document.write('
');

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.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

  1 comment:

  1. The Best Casino Games in Oklahoma
    In the gambling 룰렛 industry, there's a casino that serves as 룰렛 게임 a conduit 해외 배팅 사이트 for wagering on a wide 아시안부키 array of casino games. In order to enjoy this 마이크로게이밍 casino, you must

    ReplyDelete

Popular Posts

Powered by Blogger.