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

188
Views
cambiar el color de fondo cuando el área de texto contiene cierta palabra de la matriz

Quiero cambiar el color de fondo de los documentos a goodColor si textArea contiene más goodWords que badWords. y viceversa. Además, si la misma palabra de una matriz se ingresa dos veces, necesito que cuente como incluida dos veces.

 const goodWords = ['happy', 'joyful', 'amazing', 'enjoyed', 'fun', 'excited', 'nice', 'funny', 'fantastic', 'good', 'calm', 'comfortable', 'glad', 'confident', 'kind']; const badWords = ['angry', 'sad', 'upset', 'defeated', 'embarrassed', 'jealous', 'nervous', 'anxious', 'unhappy', 'miserable', 'worst', 'bad']; const goodColor = 'rgb(225,225,56,20)' const badColor = 'rgb(100,100,50,50)' textArea.addEventListener('input', function () { for (let good of goodWords) { if(text.value.includes(good)) { document.body.style.backgroundColor = goodColor; } } for(let bad of badWords) { if (text.value.includes(bad)) { document.body.style.backgroundColor = badColor; } } })
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Primero debe dividir el valor del área de texto por palabras y luego contar todas las coincidencias en ambas matrices. Luego, simplemente puede compararlos y colorear bg como desee.

 const goodWords = ['happy', 'joyful', 'amazing', 'enjoyed', 'fun', 'excited', 'nice', 'funny', 'fantastic', 'good', 'calm', 'comfortable', 'glad', 'confident', 'kind']; const badWords = ['angry', 'sad', 'upset', 'defeated', 'embarrassed', 'jealous', 'nervous', 'anxious', 'unhappy', 'miserable', 'worst', 'bad']; const goodColor = 'rgb(225,225,56,20)' const badColor = 'rgb(100,100,50,50)' textArea.addEventListener('input', function () { // Exit if textarea contains only spaces or empty if (text.value.trim().length === 0) { return; } // Using regexp break value to array of words const words = text.value.match(/\b(\w+)\b/g) let goodCount = 0; let badCount = 0; // Cycle through words for (let i = 0; i < words.length - 1; i++) { if (goodWords.includes(words[i])) { goodCount++; } if (badWords.includes(words[i])) { badCount++; } } if (goodCount > badCount) { document.body.style.backgroundColor = goodColor; } else if (goodCount < badCount) { document.body.style.backgroundColor = badColor; } else { // Some neutral color } })
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!