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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript output problem

    Hey, I am relatively new to javascript and I am having a problem with my output in several ways.

    The program is a class project, I have the project part done and this is now for fun and self education. The project was to take a word, say, "JAVA" and find all the permutations of the word, ex. "JVAA,VJAA,VAJA,VAAJ,JAAV,AJAV,AAJV,AAVJ,JAVA,AJVA,AVJA,AVAJ"

    I have that part working, and I technically could stop here, but what is the fun in that? I want to do this:

    1. User enters word in a textbox
    2. User clicks the button "Permute"
    3. Below the button the permutations and the number of permutations is displayed.

    I am using Firefox 2.0 for my testing, but it is supposed to work in IE 6.0

    The problems:
    1. Document.write() and Document.writeln() are erasing my page
    2. FF2: The webpage gets stuck in an infinite loop when I try to use the below code. After wiping the page clean, it displays the permutations correctly, and the number of permutations, but then it seems to think its still loading for some reason. In trying to figure out what the problem was I did a test using a textarea and output to it and it worked perfectly, except it didn't line wrap.
    3. IIE6: Outputs the permutations, then doesn't output the number of permutations.

    I realize that the problem with text area is because I just tell it to output the array and if I did a for loop I could insert spaces but one of the restrictions for the program is no for loops and I cant address the array with subscripts.

    What I need is an alternative method for outputting the text to webpage or for some kind soul to point out where my code goes wrong below.

    Code:
    <form name="form1">
        <input name="txtPerm" type="text" value="" />
        <input type="button" value="Compute" onClick="
          w = document.form1.txtPerm.value;
    	var permute = perm( w );
    	document.write('The permutations of ' + w + ' are: ' + permute + '<br /><br />');
    	document.write('There were ' + permute.length + ' permutations.<br /><br />');
            
         " />
        <br />
    </form>
    Thanks a ton for any and all help.

    P.S. I understand that some people will not want to help what they see as a homework project but I just want to make it abundantly clear that this is not part of the requirements this is just for fun and it is frustrating me to no end.

    As further proof here is a link to the assignment and for the lazy here is a cut/paste of the assignment:
    Write a JavaScript program, embedded in a HTML file that can be interpreted with Internet Explorer 6.0, that prompts the user to enter a word and displays all permutations of the word without duplicates. Use recursion instead of loops in all functions.
    Again thanks for reading this and any suggestions would be appreciated, thanks.
    Last edited by ryodoan; 07-10-2007 at 05:37 AM.

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    - use DOM methods instead of the non-dynamic document.write() method (see my next post)
    - split the input word (string) into the characters to have an array
    - the request of using a recursion instead of a loop is a little bit unusual. Anyway, if you don't know how to use a recursion in javascript, here's an example
    Code:
    function myFunction(n){
    alert(n)
    myFunction(n-1);
    }
    Sure, when called, the function will enter in an endless loop, thus you need a stop condition (let's say the alerted n value should be a positive integer, 0 included)
    Code:
    function myFunction(n){
    alert(n)
    if(n>0){
    myFunction(n-1);
    }
    }
    Usually javascript coders do not use a recursion for this type of job, they prefer a while loop
    Code:
    function myFunction(n){
    while(n>=0){
    alert(n);
    n--;
    }
    }
    Hope that helps
    Last edited by Kor; 07-10-2007 at 10:03 AM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    And here's a simple example of writing a concatenated string, using DOM method data

    object.firstChild.data
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <script type="text/javascript">
    function WriteWord(){
    var mystring='', n=0, mychar;
    while(n<=5){
    mychar=prompt('Enter a character','');
    mystring+=mychar.toString();
    n++;
    }
    document.getElementById('myspan').firstChild.data=mystring;
    }
    onload=WriteWord;
    </script>
    </head>
    <body>
    <span id="myspan">&nbsp;</span>
    </body>
    </html>
    I used a while loop, but you may use a recursion, if the teacher asked you to. I've shown you in my previous post how to use a recursion.

    Hope that helps
    Last edited by Kor; 07-10-2007 at 10:02 AM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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