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; } } })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 } })