Interview Question: Find all combinations of work

by rgthree

HTML

This is a play off of <a href="http://jsfiddle.net/rgthree/k86fH/" target="_blank">a find all valid word substrings</a> problem to further find all combinations of letter that make up a valid word within another given word.
<br><br>
Input: "hotdog"<br>
Run: <input id="output" style="width:250px;"/><br>

JavaScript

// Assuming there is already a function that validates whether a word is valid
// Mock this up for "hotdog"
var isValidWord = function(w){
  return /^(hot|dog|good|hoot|too|to|dot|do|go|goo)$/.test(w);    
}

var getAllCombos = function(word){
    var letter, combos, _combos;
    word = String(word);
    combos = [];
    if(word.length == 1){
      combos = [word];
    }else{
      word.split('').forEach(function(letter, index){
        combos.push(letter);
        _combos = getAllCombos(word.substr(0,index)+word.substr(index+1));
        _combos.forEach(function(c){
          combos.push(letter+c); 
        });
      });
    }    
    return combos;
};

var findValidWords = function(word){
    var words;
    words = [];
    getAllCombos(word).forEach(function(attempt){
        if(words.indexOf(attempt) === -1 && isValidWord(attempt))
            words.push(attempt);
    });
    return words;    
}

var validwords = findValidWords('hotdog');
document.getElementById('output').value = validwords;
console.log(validwords);