Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

134
Vistas
Check a string of random letters for an english word

I'm developing a website for fun that adds a random character to a div element on every key press. I have that part working, but what I need is for the program to check if an English word has been created out of the random characters and highlight that word. I've tried many things, and none have worked. Preferably this would highlight it regardless of whether or not there are spaces surrounding the word, but I will be very happy with really any working code.

This is my current JS for random character generation and autoscrolling, also with some code that doesn't work that is meant to search the code.

var letter = 1
var number = 1
const alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '\u0020']
function typingfunction() {
    number = Math.floor(Math.random() * 27)
    letter = alphabet[number]
    document.getElementById("typing").innerHTML = `${document.getElementById("typing").innerHTML}${letter}`
    window.scrollTo(0, document.body.scrollHeight * document.body.scrollHeight);
    var input = document.getElementById("typing").innerHTML;
    var fs = require('fs');
    fs.readFile("words_alpha.txt", function(words) {
       var words = words.toString().split('\n').filter(function(word) {
           return word.length >= 4;
    })});
    var output = [];
       words.forEach(word); {
           if (input.match(word)) {
               output.push(word);
           }
    };
    console.log(output);
}

window.addEventListener('keydown', typingfunction)
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The code for testing a word basically is OK but there was a syntax error in your code (incorrect format for an anonymous function) and also the words array was only declared inside a function but you tried to use it outside the function.

This code slightly reorganises things to take account of this.

I have no way of testing the nodejs filereading (assuming it is nodejs) so if that causes a problem add the tag to your question to get more help in that area.

The code listens for a click on the window and sets the input HTML empty so several trials can be run.

var letter = 1
var number = 1
const alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '\u0020']
function typingfunction() {
    number = Math.floor(Math.random() * 27)
    letter = alphabet[number]
    document.getElementById("typing").innerHTML = `${document.getElementById("typing").innerHTML}${letter}`
    window.scrollTo(0, document.body.scrollHeight * document.body.scrollHeight);
    var input = document.getElementById("typing").innerHTML;   
    var output = [];
       words.forEach(word => {
           if (input.match(word)) {
               output.push(word);
           }
    });
    console.log(output);
}
// first set up the array of words from the file containing them
let words; //declare words here so it can be used later on
let fs = require('fs');
    fs.readFile("words_alpha.txt", function(allWords) {
       words = allWords.toString().split('\n').filter(function(word) {
           return word.length >= 4;
    })});

window.addEventListener('keydown', typingfunction);
window.addEventListener('click', function () {document.getElementById("typing").innerHTML = '';});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda