Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    why is this backwards???

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

  • #2
    New Coder
    Join Date
    Jul 2002
    Location
    San Jose
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    what are you trying to do?

    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

  • #3
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    not what i meant

    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

  • #4
    New Coder
    Join Date
    Jul 2002
    Location
    San Jose
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    reverse in code

    Looks like you are calling the reverse display in this code:

    ("reverse.html","reverse")

  • #5
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    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


    A1ien51

  • #6
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thank you!

    Thank you very much, the last reply has rid me of my confusion, i really appreciate it.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •