View Full Version : Javascript output problem

07-10-2007, 05:24 AM
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.

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

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 (http://cheng.ececs.uc.edu/403/sy.html) 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.

07-10-2007, 09:42 AM
- 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

function myFunction(n){

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)

function myFunction(n){

Usually javascript coders do not use a recursion for this type of job, they prefer a while loop

function myFunction(n){

Hope that helps

07-10-2007, 10:00 AM
And here's a simple example of writing a concatenated string, using DOM method data


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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;
mychar=prompt('Enter a character','');
<span id="myspan">&nbsp;</span>

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