Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

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 permutationstring permutation with minor changes, but I tried something like the above one. Could you please suggest refactoring to be done for the above code?

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?

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?

deleted 16 characters in body; edited tags
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

I have tried to write a javascriptJavaScript code to check whether weI can form a word from the given string[jumbled].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);
}

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

I have tried to write a javascript code to check whether we 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);
}

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

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?

Rollback to Revision 1
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

I have tried to write a javascript code to check whether we can form a word from the given string[jumbled].

/**
* tells you whether the given word could be formed from the given jumbled string
* @param {string} word the word to be formed
* @param {string} jumbledLetters the jumbled chars
* @param {boolean} isSameLetter true if the word & jumbledLetters are same in length
*/
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++){
    // checks current chars are equal & the current char [j !== uniquePosition[j]] is not lookedup already
    if(word[i] === jumbledLetters[j] && j !== uniquePosition[j]) {
      uniquePosition[j] = j;
      break;
    }
  }
}
 
return Object.keys(uniquePosition).length === (word.length);
}

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

I have tried to write a javascript code to check whether we can form a word from the given string[jumbled].

/**
* tells you whether the given word could be formed from the given jumbled string
* @param {string} word the word to be formed
* @param {string} jumbledLetters the jumbled chars
* @param {boolean} isSameLetter true if the word & jumbledLetters are same in length
*/
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++){
    // checks current chars are equal & the current char [j !== uniquePosition[j]] is not lookedup already
    if(word[i] === jumbledLetters[j] && j !== uniquePosition[j]) {
      uniquePosition[j] = j;
      break;
    }
  }
}
 
return Object.keys(uniquePosition).length === (word.length);
}

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

I have tried to write a javascript code to check whether we 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);
}

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

added 454 characters in body
Source Link
Loading
Source Link
Loading