Skip to main content
4 of 5
deleted 16 characters in body; edited tags
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Finding the given word from jumbled letters

I have tried to write JavaScript code to check whether I can form a word from the given string (jumbled):

function findWord(word, jumbledLetters, isSameLength) {
if(isSameLength && word.length !== jumbledLetters.length)
return false;

var uniquePosition = {};
for(var i = 0; i< word.length; i++)
for(var j=0; j< jumbledLetters.length; j++){
  if(word[i] === jumbledLetters[j] && j !== uniquePosition[j]) {
    uniquePosition[j] = j;
    break;
  }
}
return Object.keys(uniquePosition).length === (word.length);
}

I could do this with string permutation with minor changes, but I tried something like the above one. Could you please suggest refactoring to be done for the above code?