...

View Full Version : why is this backwards???



sicknesslife
07-28-2002, 01:38 AM
Hello,

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>");
new_window.document.writeln(the_string);

// show me -->
</script>





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

bcuria
07-28-2002, 02: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:

http://sislands.com/coin70/answers/a2/234v1.htm

sicknesslife
07-28-2002, 02: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

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

("reverse.html","reverse")

A1ien51
07-28-2002, 07: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

:thumbsup:
A1ien51

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum