Skip to main content
edited body
Source Link
user44869
user44869

This is a JavaScript function that returns ALL the possible combinations of whatever word the user enters. I am looking to clean this code up a bit...any and all suggestions are welcome!

  function allanagramsallAnagrams (word) {
  if (word.length < 2) {
    return [word];
  } else {
      var allanswersallAnswers = [];
      for (var i = 0; i < word.length; i++) {
        var letter = word[i];
        var shorterwordshorterWord = word.substr(0, i) + word.substr(i + 1, word.length - 1);
        var shortwordarrayshortwordArray = allanagramsallAnagrams(shorterwordshorterWord);
        for (var j = 0; j < shortwordarrayshortwordArray.length; j++) {
          allanswersallAnswers.push(letter + shortwordarray[j]shortwordArray[j]);
        }
      }
      return allanswers;allAnswers;
  }
}

allanagramsallAnagrams("")

This is a JavaScript function that returns ALL the possible combinations of whatever word the user enters. I am looking to clean this code up a bit...any and all suggestions are welcome!

  function allanagrams (word) {
  if (word.length < 2) {
    return [word];
  } else {
      var allanswers = [];
      for (var i = 0; i < word.length; i++) {
        var letter = word[i];
        var shorterword = word.substr(0, i) + word.substr(i + 1, word.length - 1);
        var shortwordarray = allanagrams(shorterword);
        for (var j = 0; j < shortwordarray.length; j++) {
          allanswers.push(letter + shortwordarray[j]);
        }
      }
      return allanswers;
  }
}

allanagrams("")

This is a JavaScript function that returns ALL the possible combinations of whatever word the user enters. I am looking to clean this code up a bit...any and all suggestions are welcome!

  function allAnagrams (word) {
  if (word.length < 2) {
    return [word];
  } else {
      var allAnswers = [];
      for (var i = 0; i < word.length; i++) {
        var letter = word[i];
        var shorterWord = word.substr(0, i) + word.substr(i + 1, word.length - 1);
        var shortwordArray = allAnagrams(shorterWord);
        for (var j = 0; j < shortwordArray.length; j++) {
          allAnswers.push(letter + shortwordArray[j]);
        }
      }
      return allAnswers;
  }
}

allAnagrams("")
shorten title
Link
David Harkness
  • 8.8k
  • 1
  • 26
  • 44

JavaScript function that returns Generate all possible combinations of letters in a word

edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
Source Link
user44869
user44869
Loading