Jump to content

Can't find the problem


DocGrimwig

Recommended Posts

So here is my program to take a user input, process it for all possible word combinations and then output the resulting string array to a p string

 

I have been working on this for weeks and was sure I had finally gotten it. Can anyone help?

 

Thanks!

Doc

 

 

<codeblock>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Word Combinations</title>
</head>
<body>
<h3>Please input a word to be processed for all of its possible combinations:</h3>
<input id="userInput" type="text" autofocus="autofocus">
<button id="processInput" onClick="getAnagrams("userInput")">Process</button>
<p id="returnValue"></p>
<script>
function swap(chars, i, j) {
var tmp = chars;
chars = chars[j];
chars[j] = tmp;
}
function getAnagrams(input) {
//initialize the variables
var counter = [],
anagrams = [],
chars = input.split(''),
length = chars.length,
i;
//determine how many letters are in the string
for (i = 0; i < length; i++) {
counter = 0;
}
// for loop 1st it will set i to 0; then if i < 3 (using Dog); increment by 1's
//next loop sets i to 1; i is still less than 3; so increment by 1 again
//next loop sets i to 2; i is still less than 3; so increment by one again
//next loop sets i to 3; i is NO LONGER less than 3 so loop stops and returns a string length of 3 (0,1,2 = 3 in the array)
//push the original string in container
anagrams.push(input);
//the (input) string 'dog' is put into the anagrams container
//start from first letter
i = 0;
while (i < length) {
if (counter < i) {
// i is now = to 0 in the array = 'd'
// WHILE i < 3 and IF the loop counter is less than 3 then perform the following
//swap the two letters in questions
swap(chars, i % 2 === 1 ? counter : 0, i);
//increment counter
counter++;
i = 0;
//push new combination into container
anagrams.push(chars.join(''));
} else {
counter = 0;
//increment counter
i++;
}
}
//return string of results
// return anagrams;
//Place the result of anagrams into the p id = "returnValue"
document.getElementById("returnValue").innerHTML = anagrams;
}
</script>
</body>
</html>
</codeblock>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...