Help with str.split and show permutations.

B

bH

Hi All,
Below is my efforts to show the recursive permutation of letters
in a single word.

I am using Windows XP and IE Browser

I am unsuccessful for 2 reasons:
The line number 22 reads as:
sub_permute(str.split(""), 0, str.length);
An error is noted "object does not support this property or method"

And second I need to declare an "out" that will show the
various permutations as an alert or within a text area.

TIA
bH

<html>
<head>
<title>PermuteAlpha</title>
<script type="text/javascript">

function PermuteAlpha() {
var str = document.getElementById("lttrsStr");
var ret = [];
function sub_permute(a, m, n){
var i, t;
if (m<n-1) {
sub_permute(a, m+1, n);
for (i=m+1;i<n;i++) {
t=a[m], a[m]=a, a=t;
sub_permute(a, m+1, n);
t=a[m], a[m]=a, a=t;
}
} else {
ret.push(a.join(""));
}
}
sub_permute(str.split(""), 0, str.length);
return ret;
}

</script>
</head>
<body>
<form>
<input maxLength=25 value="" type="text" id="lttrsStr"/><BR>
<button onclick= "PermuteAlpha()">Permute</button>
</form>
</body>
</html>
 
V

VK

Hi All,
Below is my efforts to show the recursive permutation of letters
in a single word.

I am using Windows XP and IE Browser

I am unsuccessful for 2 reasons:
The line number 22 reads as:
sub_permute(str.split(""), 0, str.length);
An error is noted "object does not support this property or method"

And second I need to declare an "out" that will show the
various permutations as an alert or within a text area.

TIA
bH

<html>
<head>
<title>PermuteAlpha</title>
<script type="text/javascript">

function PermuteAlpha() {
var str = document.getElementById("lttrsStr");
var ret = [];
function sub_permute(a, m, n){
var i, t;
if (m<n-1) {
sub_permute(a, m+1, n);
for (i=m+1;i<n;i++) {
t=a[m], a[m]=a, a=t;
sub_permute(a, m+1, n);
t=a[m], a[m]=a, a=t;
}
} else {
ret.push(a.join(""));
}
}
sub_permute(str.split(""), 0, str.length);
return ret;

}

</script>
</head>
<body>
<form>
<input maxLength=25 value="" type="text" id="lttrsStr"/><BR>
<button onclick= "PermuteAlpha()">Permute</button>
</form>
</body>
</html>


Comments are inside the code. Also be careful with the probe length:
you have a permutation jump on >= 5 items and without flow control it
may hang up your browser.

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<title>PermuteAlpha</title>
<script type="text/javascript">

function PermuteAlpha(str, out) {

out.value = "";

var ret = [];
var arg = [];
// here was your error: str.split("")
for (var i=0; i<str.length; i++) {
arg.push(str.charAt(i));
}

sub_permute(arg, 0, str.length);
return ret;

function sub_permute(a, m, n) {
var i, t, perm;
if (m<n-1) {
sub_permute(a, m+1, n);
for (i=m+1;i<n;i++) {
t=a[m], a[m]=a, a=t;
sub_permute(a, m+1, n);
t=a[m], a[m]=a, a=t;
}
} else {
perm = a.join("");
ret.push(perm);
// that is the output extension:
out.value+= perm+"\n";
}
}
}
</script>
</head>
<body>
<form action="" onsubmit="return false;">
<fieldset>
<legend>PermuteAlpha</legend>
<label for="lttrsStr">Input:</label><br>
<input type="text" maxlength="4" value="" name="lttrsStr"
id="lttrsStr">
<br>
<label for="out">Output:</label><br>
<textarea name="out" id="out" rows="10" cols="20"></textarea>
<br>
<button type="button" onclick="
PermuteAlpha(this.form.lttrsStr.value, this.form.out);
">Permute</button>
</fieldset>
</form>
</body>
</html>
 
B

bH

Hi All,
Below is my efforts to show the recursive permutation of letters
in a single word.
I am using Windows XP and IE Browser
I am unsuccessful for 2 reasons:
The line number 22 reads as:
  sub_permute(str.split(""), 0, str.length);
An error is noted "object does not support this property or method"
And second I need to declare an "out" that will show the
various permutations as an alert or within a text area.

<html>
  <head>
  <title>PermuteAlpha</title>
  <script type="text/javascript">
  function PermuteAlpha() {
  var str = document.getElementById("lttrsStr");
  var ret = [];
  function sub_permute(a, m, n){
    var i, t;
    if (m<n-1) {
      sub_permute(a, m+1, n);
      for (i=m+1;i<n;i++) {
        t=a[m], a[m]=a, a=t;
        sub_permute(a, m+1, n);
        t=a[m], a[m]=a, a=t;
      }
    } else {
      ret.push(a.join(""));
    }
  }
  sub_permute(str.split(""), 0, str.length);
  return ret;

</script>
  </head>
  <body>
  <form>
  <input maxLength=25 value="" type="text" id="lttrsStr"/><BR>
   <button onclick= "PermuteAlpha()">Permute</button>
  </form>
  </body>
  </html>


Comments are inside the code. Also be careful with the probe length:
you have a permutation jump on >= 5 items and without flow control it
may hang up your browser.

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type"
 content="text/html; charset=iso-8859-1">
<title>PermuteAlpha</title>
<script type="text/javascript">

function PermuteAlpha(str, out) {

 out.value = "";

 var ret = [];
 var arg = [];
// here was your error: str.split("")
 for (var i=0; i<str.length; i++) {
  arg.push(str.charAt(i));
 }

 sub_permute(arg, 0, str.length);
 return ret;

 function sub_permute(a, m, n) {
  var i, t, perm;
  if (m<n-1) {
   sub_permute(a, m+1, n);
   for (i=m+1;i<n;i++) {
    t=a[m], a[m]=a, a=t;
    sub_permute(a, m+1, n);
    t=a[m], a[m]=a, a=t;
   }
  } else {
   perm = a.join("");
   ret.push(perm);
// that is the output extension:
   out.value+= perm+"\n";
  }
 }}

</script>
</head>
<body>
<form action="" onsubmit="return false;">
 <fieldset>
 <legend>PermuteAlpha</legend>
 <label for="lttrsStr">Input:</label><br>
 <input type="text" maxlength="4" value="" name="lttrsStr"
id="lttrsStr">
 <br>
 <label for="out">Output:</label><br>
 <textarea name="out" id="out" rows="10" cols="20"></textarea>
 <br>
 <button type="button" onclick="
  PermuteAlpha(this.form.lttrsStr.value, this.form.out);
 ">Permute</button>
 </fieldset>
</form>
</body>
</html>- Hide quoted text -

- Show quoted text -


Hi VK,

Thanks for your efforts! Works great!

I appreciate it.

bH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,089
Latest member
Ketologenic

Latest Threads

Top