1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
var wordFound = ''
var copyArr = []
var lettersChosen = ['A', 'S', 'V', 'T', 'S', 'E', 'A', 'M', 'N', 'T']
function isAnagram(stringA, stringB) {
stringA = stringA.toLowerCase().replace(/[\W_]+/g, "");
stringB = stringB.toLowerCase().replace(/[\W_]+/g, "");
const stringASorted = stringA.split("").sort().join("");
const stringBSorted = stringB.split("").sort().join("");
return stringASorted === stringBSorted;
}
function checkForEachWord(arr) {
strLetters = ''
for (i in arr)
strLetters = strLetters + arr[i]
for (var i in file)
if (isAnagram(strLetters, file[i])) {
wordFound = file[i]
return true
}
return false
}
function getOneOfTheLongestWord() {
lettersChosen.forEach(letter => {
copyArr.push(letter)
})
var index = 1
var countLetter = 1
var position = copyArr.length - index
var savedArray = []
var iteration = 0
var test = checkForEachWord(copyArr)
if (test == true)
return true
while (test == false) {
copyArr.splice(position, 1)
index++
if (index > copyArr.length + 1) {
index = 1
countLetter++
}
console.log(copyArr + ' | ' + position)
position = copyArr.length - index
test = checkForEachWord(copyArr)
copyArr = []
lettersChosen.forEach(letter => {
copyArr.push(letter)
})
}
return true
}
getOneOfTheLongestWord() |
Partager