View Full Version : why is this backwards???

07-28-2002, 12:38 AM

I am doing an online javascript tutorial and i have the following code:

<script language="JavaScript">
<!-- hide me

var the_number = prompt("how many words (3-5 is good)?", "4");
var the_string = "";
var a_word;

for (loop = 0; loop < the_number; loop++)
line_number = loop + 1;
a_word = prompt("what's word " + line_number + "?","");
the_string = "word " + line_number + ": " + a_word + "<br>" + the_string;

var new_window = window.open("reverse.html","reverse","height=400,width=400");
new_window.document.writeln("<h1>In reverse</h1>");

// show me -->

I can't figure out why this code is displaying the words i put in in the reverse order that i put them in, can someone please help....

07-28-2002, 01:35 AM
If you are trying to display a list of words in reverse order, here is the answer to a similar assignment I had recently:


07-28-2002, 01:44 AM
My script is putting the words i enter on the page in reverse order. I don't want this, i would appreciate it if someone could explain why this is happening

07-28-2002, 02:04 AM
Looks like you are calling the reverse display in this code:


07-28-2002, 06:07 AM
The problem is with this line
the_string = "word " + line_number + ": " + a_word + "<br>" + the_string;

what it is doing is adding the code of the new word in front of the old string. This is causing the inputs to be reversed. (it is piling the information right on top of each other)

now what you need to do is this

the_string = the_string + "word " + line_number + ": " + a_word + "<br>";

See how I moved the the_string to the front?
Now it will appear in the correct order


07-30-2002, 07:16 PM
Thank you very much, the last reply has rid me of my confusion, i really appreciate it. :D