JAVASCRIPT: I am trying to count the number of repeated letters, count number of non white space characters, and all characters including white space

Viewed 20

This is currently my code: It counts all words, however when I try to make it count all non white spaces or letters in a sentence it does not work. I am very new to Java script so all replies will help!

<body>
    <p>
    <textarea id="textBubble" height="500"></textarea>
    </p>
    <p>
    <span id="charCount">0</span> Characters
    </p>
    <p>
    <span id="nonWhiteandWhiteCharCount">0</span> Non White and White Characters
    </p>
    <button type="button">Submit!</button>
    <script>
    var textBubble = document.getElementById("textBubble");
    var charCount = document.getElementById("charCount");
    var nonWhite = document.getElementById("textBubble")
    textBubble.addEventListener("keyup",function(){
    var char = textBubble.value.split(/\s+/);
    charCount.innerText = char.length;
    var non = textBubble.value.split('');
    nonWhite.innerText = characters.filter( item => {
    return (item != ' ');
    }).length;

    nonWhite.innerText = non.length;
      });

     function duplicateCount(text) {
     i = i.toLowerCase()
     let obj = {}
     for (let i of text) {
     if (!obj[i]) {
     obj[i] = 1
     }
     }
     for (let i in obj) {
     if(obj[i] > 1) {
     count++
     }
     }
     return count
     }
     </script>
     </body>
1 Answers

Not sure if it will solve your problem but I've noticed two errs:

  1. var nonWhite = document.getElementById("textBubble") I guess you wanted to use different id here, because you've already used "textBubble"
  2. If you want to add event listener function like this, change it to ()=>{/*put your whole function here*/}
Related