-2

I implemented a Java code for permutation combination a string with input str= "word1 word2 word3" then output as:

arr[0]= "word1 word2"

arr[1]= "word1 word3"

arr[2]= "word1 word2 word3"

arr[3]= "word2 word1"

arr[4]= "word2 word1 word3"

arr[5]= "word2 word3 word1"

arr[6]= "word3 word1"

arr[7]= "word3 word1 word2"

arr[8]= "word3 word2 word1"

This is Java code:

private static void permute(String[] ss, boolean[] used, String res, int level, List<String> list) {

    if (level == ss.length && res != ""){
        list.add(res);
    return;
    }

    for (int i = 0; i < ss.length; i++) {
        // Check if the string is currently used
        if (used[i]) {
            continue;
        }
        // Check if res is empty or a single word
        if(level > 1)
            list.add(res);
            used[i] = true;
            permute(ss, used, res + " " + ss[i], level + 1, list);
            used[i] = false;
        }
    }

public static List<String> PermuteWords(String s){
    String[] ss = s.split(" ");
    boolean[] used = new boolean[ss.length];
    String res = "";
    List<String> list = new ArrayList<String>();
    permute(ss, used, res, 0, list);
    return list;
}

When converting to JS code, I don't know what are errors in this code:

function permute(ss, used, res, level, list){
  if(level==ss.lenght&&res!==""){
      list.add(res);
      return;
  }

  for(var i=0; i<ss.lenght; i++){
      if (used[i]===true){
        continue;
      }

      if(level>1){
        list.add(res);
        used[i]=true;
        permute(ss, used, res+" "+ss[i], level+1, list)
        used[i]=false;
      }
    }
}
function permuteword(s){
  var ss;
  for(var i=0; i<s.length;i++){
      ss[i]=s[i];
  }
  var used;
  for(var j=0; j<s.length;j++){
      used[j]=false;
  }
  var result;
  permute(ss, used, res, 0, result);
  return result;
}
Nguyen Chi Hieu
  • 98
  • 1
  • 11

2 Answers2

1

Java and Javascript may sound almost the same but they aren't. They take different appoaches doing different things. If in Java You are checking the console for errors on running the program, then in Javascript you can check for the errors in the browser console (open console with F12)

POPULATE ARRAY
Javascript arrays don't have add() method. To add a value to it, use push:
list.push(res)

POPULATE ARRAY #2
In permuteword() function you are trying to populate variable ss that is not initialized. Compiler doesn't understand where you want to put that value. Initialize ss as an empty array:
var ss = [];

TYPOs
In the first for loop you have ss.lenght. Fix that. Always check for the typos.

EXTRA
In permuteword() you are passing res to the permute() function although you don't have it defined in the function. Things will work if res is a global variable defined outside of the functions.


And take advantage of that browser console. Every Javascript developer (from noobie to pro) has it always open!

Mike B
  • 2,756
  • 2
  • 16
  • 28
0

I ran the code in couchdb and cloudant that showed the general errors that i missed checking the syntax. I modified the code as:

function permute(ss, used, res, level, list){
  if(level==ss.lenght&&res!==""){
      list.push(res);
      return;
  }

  for(var i=0; i<ss.length; i++){
      if (used[i]===true){
        continue;
      }

      if(level>1){
        list.push(res);
        used[i]=true;
        permute(ss, used, res+" "+ss[i], level+1, list)
        used[i]=false;
      }
    }
}
function permuteword(s){
  var ss=[];

  for(var i=0; i<s.length;i++){
      ss[i]=s[i];
  }

  var used=[];
  for(var j=0; j<s.length;j++){
      used[j]=false;
  }
  res="";

  var result=[];
  permute(ss, used, res, 0, result);
  return result;
}

function(doc){
  var list=permuteword("word1 word2 word3");
  for(var i=0; i<list.length; i++){
      index("default", list[i],{store, true});
  }
}

However, i still meet the errors as:

{"error":"{nocatch,{compilation_error,<<\"Expression does not eval to a function. ((new String(\\"function permute(ss, used, res, level, list){\\r\\n if(level==ss.lenght&&res!==\\\\"\\\\"){\\r\\n list.push(res);\\r\\n return;\\r\\n }\\r\\n\\r\\n for(var i=0; i1){\\r\\n list.push(res);\\r\\n used[i]=true;\\r\\n permute(ss, used, res+\\\\" \\\\"+ss[i], level+1, list)\\r\\n used[i]=false;\\r\\n }\\r\\n }\\r\\n}\\r\\nfunction permuteword(s){\\r\\n var ss=[];\\r\\n ss=s.split(\\\\" \\\\");\\r\\n var used=[];\\r\\n for(var j=0; j>}}","reason":"[{couch_os_process,prompt,3,[{file,\"src/couch_os_process.erl\"},{line,65}]},\n {dreyfus_index_updater,update,2,\n [{file,\"src/dreyfus_index_updater.erl\"},{line,42}]}]"}

markwatsonatx
  • 3,391
  • 2
  • 21
  • 19
Nguyen Chi Hieu
  • 98
  • 1
  • 11
  • don't write your own code as answers. You can edit the question or add comments. – Mike B Apr 25 '16 at 16:37
  • And see [this post's](http://stackoverflow.com/questions/9034284/couchdb-map-in-design-document-gives-compilation-error) accepted answer to see what you have to change to escape couchdb errors. The code itself is good now. – Mike B Apr 25 '16 at 16:41
  • Does the code work for you now? You should take an action with this post. Ask more or accept my answer, that covers the question. – Mike B Apr 26 '16 at 07:41
  • @Mikelis it does not work in cloudant. I also tried with a small sample and it works in small sample. So I think the return of permuteword still has errors but i dont know how to fix. – Nguyen Chi Hieu Apr 26 '16 at 08:46
  • The last function doesn't have a name – Mike B Apr 26 '16 at 08:50
  • and you don't have a function `index()`. There is an error when you are calling this - `index("default", list[i],{store, true});` – Mike B Apr 26 '16 at 08:53
  • No, the last function is right. It is a function of Search Index for document in clouddant (I also checked,). I want to use permuteword to index the content of document that a query with many input cases can search the document. – Nguyen Chi Hieu Apr 26 '16 at 08:56
  • And also - this - `{store, true}` is just wrong syntax. If you want array, use [store, true], if something else, explain what it is. And you should really put the code together peace by peace and check for the error. Not the whole block of code in once. – Mike B Apr 26 '16 at 08:56
  • No, the syntax of last function is right totally. I also tagged "cloudant" but no expert in cloudant view my question so far. – Nguyen Chi Hieu Apr 26 '16 at 08:59
  • And if these are some kind of built in functions for cloudant, then that is a different question. Normal JS does not reognize them. I've corrected all your JS synthax mistakes (including my last comments here), so this one should be closed. – Mike B Apr 26 '16 at 09:00
  • Yeah, thanks you. Cloudant supports js as well, it works when I run a small sample. This question is related with js and cloudant. Maybe I will ask another question with full description. Also i will check the code and close when it work. – Nguyen Chi Hieu Apr 26 '16 at 09:07
  • the problem is that in pure javascript, when i am checking your code, I cannot define function like the last one. And I don't have that `index` function. After removing those, everything works fine (synthax-wise). [See the fiddle](https://jsfiddle.net/bnb53qyd/) So i'd close this one.. – Mike B Apr 26 '16 at 09:10
  • @Mikelis I tried but when i printed out the list of permuteword nothing in output. – Nguyen Chi Hieu Apr 26 '16 at 09:21
  • of course, because the function is not called. (Because I said that the index function doesn't work in the pure javascript). And the list is not global var. But this question was about **syntax** – Mike B Apr 26 '16 at 09:23
  • I tried your link and modified the code to print list[i] for only test permuteword (not in cloudant). But the output is nothing. – Nguyen Chi Hieu Apr 26 '16 at 09:27
  • I tried with this: function daa() { var list=permuteword("word1 word2 word3"); for(var i=0; i – Nguyen Chi Hieu Apr 26 '16 at 09:39
  • then this is a question about the functionality, not the syntax. – Mike B Apr 26 '16 at 09:41
  • but with no error in syntax, the code doesn't work in pure js. – Nguyen Chi Hieu Apr 26 '16 at 09:44
  • I've exaplined your steps [in this chat](http://chat.stackoverflow.com/rooms/110239/discussion-between-mikelis-baltruks-and-nguyen-chi-hieu). – Mike B Apr 26 '16 at 09:47
  • Thanks you, i updated a new code as your suggestion. It works well. – Nguyen Chi Hieu Apr 26 '16 at 10:37