I am writing a program in JavaScript to find all possible words given 7 letters. The words have to be 3 or more characters, and for right now I am limiting it to 7 characters long. The code below works, but takes forever to complete and I would like to speed it up. Would recursion help?
Thanks for any suggestions.
The variable wordList is an array of 60,000 words.
function isWord(word){
var found = wordList.indexOf(word);
if(wordList[found]===word) return true;
else return false;
}
for(var i=0; i<letters.length; i++) {
for(var j=0; j<letters.length; j++) {
for(var k=0;k<letters.length; k++) {
if(isWord(letters[i]+letters[j]+letters[k]))
w.push(letters[i]+letters[j]+letters[k])
}
}
}
for(var i=0; i<letters.length; i++) {
for(var j=0; j<letters.length; j++) {
for(var k=0; k<letters.length; k++) {
for(var l=0; l<letters.length; l++) {
if(isWord(letters[i]+letters[j]+letters[k]+letters[l]))
w.push(letters[i]+letters[j]+letters[k]+letters[l])
}
}
}
}
for(var i=0; i<letters.length; i++) {
for(var j=0; j<letters.length; j++) {
for(var k=0; k<letters.length; k++) {
for(var l=0; l<letters.length; l++) {
for(var m=0; m<letters.length; m++) {
if(isWord(letters[i]+letters[j]+letters[k]+letters[l]+letters[m]))
w.push(letters[i]+letters[j]+letters[k]+letters[l]+letters[m])
}
}
}
}
}
for(var i=0; i<letters.length; i++) {
for(var j=0; j<letters.length; j++) {
for(var k=0; k<letters.length; k++) {
for(var l=0; l<letters.length; l++) {
for(var m=0; m<letters.length; m++) {
for(var n=0; n<letters.length; n++) {
if(isWord(letters[i]+letters[j]+letters[k]+letters[l]+letters[m]+letters[n]))
w.push(letters[i]+letters[j]+letters[k]+letters[l]+letters[m]+letters[n])
}
}
}
}
}
}
for(var i=0; i<letters.length; i++) {
for(var j=0; j<letters.length; j++) {
for(var k=0; k<letters.length; k++) {
for(var l=0; l<letters.length; l++) {
for(var m=0; m<letters.length; m++) {
for(var n=0; n<letters.length; n++) {
for(var o=0; o<letters.length; o++) {
if(isWord(letters[i]+letters[j]+letters[k]+letters[l]+letters[m]+letters[n]+letters[o]))
w.push(letters[i]+letters[j]+letters[k]+letters[l]+letters[m]+letters[n]+letters[o])
}
}
}
}
}
}
}