Here is my problem:
For a given sentence, find occurrences of the given character set. Pick filtered words and generate permutations. (SENTENCE AND CHARACTER SET ONLY CAN HAVE UPPERCASE LETTERS)
Show selected words & permutations
Example:
Inputs
sentence = "THIS IS AN ISSUE FROM JOHN"
word = "IS"
Output:
{
words: ["THIS", "IS", "ISSUE"],
permutations: [["THIS","IS","ISSUE"], ["IS","THIS","ISSUE"], ["ISSUE","THIS","IS"], ["THIS","ISSUE","IS"], ["IS","ISSUE","THIS"], ["ISSUE","IS","THIS"]]
}
And the following criteria should be fulfilled:
Return type should be an object.
getPermutations("THIS IS AN ISSUE FROM GIHAN", "IS")
Should return:
{ "word": ["THIS","IS","ISSUE"], "permutations": [["THIS","IS","ISSUE"], ["IS","THIS","ISSUE"], ["ISSUE", "THIS", "IS"], ["THIS", "ISSUE", "IS"], ["IS", "ISSUE", "THIS"], ["ISSUE","IS","THIS"]] }
Answer should be valid for any given input.
Here is my code:
function getPermutations(sentence, word) {
var words = sentence.split(" ");
var x = words[0] +" "+ words[1] +" "+ words[3];
var inputArray = x.split(" ");
var permutations = inputArray.reduce(function permute(res, item, key, arr) {
return res.concat(arr.length > 1 && arr.slice(0, key).concat(arr.slice(key + 1)).reduce(permute, []).map(function(perm) { return [item].concat(perm); }) || item);
}, []);
var output = { words: words,permutations : permutations};
return output;
}
console.log(getPermutations("THIS IS AN ISSUE FROM JOHN", "IS"));
there is some error so it confusing with this. Any suggestion please?