Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!