I'm a beginner in JavaScript and tried making a simple Anagram generator that, after all the possible permutations are generated, checks if the words I just generated are valid Italian words. My problem is that when I try to convert the response from a "dictionary" file I found online into an array (don't know if that's necessary, but wanted to try this way), the Google Chrome developer console gives me the below error:
index.html:24 Uncaught TypeError: Cannot read property 'split' of undefined at index.html:24
I'm really confused, because if I copy and paste the 24th line from my code into the console (changing the variable name) and then execute it, it works just fine.
Am I missing something? I really don't know how to solve that.
Here's the code:
let dict = resp.responseText.split("\n"); // <- LINE 24
let words = [];
let generate = function(k, A) {
if (k == 1) words.push(A.join(""));
else generate(k - 1, A);
for (let i = 0; i < k - 1; i++) {
if (k & 0) {
swap(A, i, k - 1);
} else {
swap(A, 0, k - 1);
}
generate(k - 1, A);
}
}
let anagram = function() {
let chars = document.getElementById("w").value.split("");
let len = chars.length;
if (len >= 6) alert("Usa meno di 6 caratteri")
else generate(chars.length, chars);
//let p = document.getElementById("output");
//p.innerHTML = "";
}
let swap = function(arrStr, a, b) {
let tmpVal = arrStr[a];
arrStr[a] = arrStr[b];
arrStr[b] = tmpVal;
return arrStr;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
let resp = jQuery.ajax({
url: 'https://raw.githubusercontent.com/napolux/paroleitaliane/master/paroleitaliane/660000_parole_italiane.txt',
});
</script>
<input type="text" id="w" onchange="anagram();" />
<br>
<p id="output"></p>