skip to Main Content

so I’m trying to create func to find 3 letters words for a long text.

const fullstory =
  "One advanced etc etc"
document.querySelector("#story").innerHTML = fullstory;

const cutspace = fullstory.split(" ");

document.querySelector("#three").addEventListener("click", find3);

 function find3() {
  for (i = 0; i < cutspace.length; i++)
      if (cutspace[i].length<=3){
        //cutspace[i].style  // not working    
    };
}
 

what am i missing?

2

Answers


  1. It seems like you’re trying to change the style of words in your text with a length less than or equal to 3 characters.

    "Since ‘cutspace[i]’ is an array, directly changing its style using ‘.style’ is not applicable. To address this, you should restore the array value with the desired style and then proceed with the necessary modifications."

    I believe the following code snippet may be useful for your solution.

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Count characters of string</title>
        <style>
            .red-text {
                color: red;
            }
        </style>
    </head>
    
    <body>
        <div id="story"></div>
        <button id="three">Three</button>
    </body>
    <script>
        const fullstory = "One advanced etc etc";
        document.querySelector("#story").innerHTML = fullstory;
    
        const cutspace = fullstory.split(" ");
    
        document.querySelector("#three").addEventListener("click", find3);
    
        function find3() {
            for (i = 0; i < cutspace.length; i++)
                if (cutspace[i].length <= 3) {
                    console.log(cutspace[i])
                    cutspace[i] = `<span class="red-text">${cutspace[i]}</span>`;
                };
            console.log(cutspace); // you will get array with red-text css addded.
            document.querySelector("#story").innerHTML = cutspace.join(" ");
        }
    </script>
    
    </html>
    
    Login or Signup to reply.
  2. The below code uses a regex match to find all the three-letter words in a given text.

    function findAllThreeLetterWords(text) {
      const words = text.match(/bw{3}b/g) || [];
      return words;
    }
    
    const longText = "This is a sample text with some three-letter words like cat, dog, and hat.";
    const result = findAllThreeLetterWords(longText);
    
    console.log(result);
    function highlightThreeLetterWords() {
      const textElement = document.getElementById('text');
      const text = textElement.textContent || textElement.innerText;
    
      // Use a regular expression to match all three-letter words
      const words = text.match(/bw{3}b/g) || [];
    
      // Highlight each three-letter word
      const highlightedText = words.reduce((acc, word) => {
        return acc.replace(new RegExp(`\b${word}\b`, 'g'), `<span class="highlight">${word}</span>`);
      }, text);
    
      // Set the updated HTML content
      textElement.innerHTML = highlightedText;
    }
    window.onload = highlightThreeLetterWords;
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Highlight Three-Letter Words</title>
      <style>
        .highlight {
          color: red;
          font-weight: bold;
        }
      </style>
    </head>
    <body>
    
    <p id="text">This is a sample text with some three-letter words like cat, dog, and hat.</p>
    
    
    <body>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search