Jump to content

Ironing out my output


DocGrimwig

Recommended Posts

I am totally new to programming and am trying to adapt a snippet that was written to the console so that it now takes a user input and outputs to the browser. I have searched w3schools and other helps, but can't seem to get this last bit done. I don;t have any syntax issues apparently, and haven't earned to debug yet, so I've hit a wall. Can you help?

 

<codebox>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Possible Word Combinations</title>
</head>
<body>
<h4>Please input a word below</h4>
<input type="text" id="userInput" >
<button onclick="scramble()">Submit</button>
<p id="inputValue"></p>
<script>
function scramble(word) {
var word = document.getElementById("UserInput")
var words = [],
rearrangedWord, head, tail;
if (!word) {
return words;
}
function rearrange(str, prefix) {
prefix = prefix || '';
str.split('').map(function(head, idx) {
tail = str.slice(0, idx) + str.slice(idx + 1);
rearrangedWord = prefix + head + tail;
if (words.indexOf(rearrangedWord) < 0) {
words.push(rearrangedWord);
}
if (tail.length > 1) {
rearrange(tail, prefix + head);
}
});
}
rearrange(word, '');
return words;
}
document.getElementById("inputValue").innerHTML = words;
(scramble("userInput"));
</script>
</body>
</html>
</codebox>
Link to comment
Share on other sites

You have a function scramble(). It has a parameter "word", except that in the very first line of the function, you're overwriting that parameter so it's useless.

 

Then you're calling the function, but not doing anything with its return value.

 

This would probably improve your code a bit, but it still has problems:

document.getElementById("inputValue").innerHTML = scramble();

Also, Javascript is case sensitive, this is a problem:

document.getElementById("UserInput")

<input type="text" id="userInput" >

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