Word Ladder

by LyndseyB

Babel + JSX

const dictionary = ['hot','dot','dog','lot','log','cog'];

const wordLadder = function(beginWord, endWord, wordList) {  
	const alphabet = 'abcdefghijklmnopqrstuvwxyz';  
  const sequences = [];
  
  const ladder = function(current, end, sequence = []) { 	
  	const seq = sequence.slice();    
    
    seq.push(current);

    Array.from(alphabet).forEach((char, c) => {
      Array.from(current).forEach((letter, i) => {  	
     		const word = current.substring(0, i) + char + current.substring(i + 1);
 
        if(wordList.includes(word) && !seq.includes(word)) {            	
          if(word === end) {
          	seq.push(end);
            sequences.push(seq);   
          } else {         
        		ladder(word, end, seq);
          }
        }
      });
    });    
  };
  
	ladder(beginWord, endWord);
  
  return sequences;
};

const result = wordLadder('hit', 'cog', dictionary);
console.log(result);