‘’’ javascript
let palabras = ;
let palabraActual = “”;
async function setup() {
createCanvas(800, 500);
try {
const respuesta = await fetch(“palabras.txt”);
const texto = await respuesta.text();
palabras = texto
.split(/\r?\n/)
.map(p => p.trim())
.filter(p => p.length > 0);
console.log(“Palabras cargadas:”, palabras.length);
} catch (error) {
console.error(“Error leyendo palabras.txt:”, error);
}
}
function draw() {
background(240);
fill(0);
textAlign(CENTER, CENTER);
textSize(60);
text(palabraActual, width / 2, height / 2);
}
function mousePressed() {
if (palabras.length > 0) {
const indice = floor(random(palabras.length));
palabraActual = palabras[indice];
console.log(
“Índice:”, indice,
“Palabra:”, palabraActual
);
}
}
‘’’
Read error with TXT and CSV files
When loading a “palabras.txt” or “palabras.CSV” file containing 69,327 words using the loadStrings or loadTable function, it indicates that the file has no records and displays the error: “SyntaxError: expected expression, got keyword ‘do’ at undefined:965:8”.
If I use the command await fetch("palabras.txt");, it reads all the data from the file, although it still displays the error: SyntaxError: expected expression, got keyword ‘do’ at undefined:965:8.