0

I'm writing a code where users can input any text and choose any letter to see how many times it occurred in that particular text - I'm not sure where I'm going wrong

function textOccurrences() {
  var inputField1 = (document.getElementById("inputField1").value);
  var inputField2 = (document.getElementById("inputField2").value);
  var count = 0;

  for (i = 0; i < length; i++) {
    if (parseInt(search) != -1) {
      count++;
      var search = inputField1.indexOf(inputField2, parseInt(search) + 1);
    }
    document.getElementId("answer").value = inputField2 + "Occurs" + count + "times";
  }
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title> Text Occurrences </title>
  <h1> Number of Character Occurrences</h1>
  <script="text/javascript"> </script>
</head>

<body>
  <form>
    <p>Enter text: </p>
    <p> <input id="inputField1" type="text" /> </p>

    <p>Enter any character:</p>
    <p> <input id="inputField2" type="text" /> </p>
    <input type="button" value="Search" onlick="textOccurrences()" />
    </p>
    <p>Number of Occurrences:</p>
    <p><textarea id="answer"></textarea></p>
  </form>
</body>

</html>
user3483203
  • 50,081
  • 9
  • 65
  • 94
Manny0
  • 17
  • 1
  • 4
  • 1
    Possible duplicate of [How to count string occurrence in string?](https://stackoverflow.com/questions/4009756/how-to-count-string-occurrence-in-string) – Striped Mar 26 '18 at 16:07
  • 2
    well, you're using search before you've declared it. – zfrisch Mar 26 '18 at 16:07
  • change `onlick="textOccurrences()"` to `onclick="textOccurrences()"`, that is the reason nothing happened when you click **Search** button. And you need to declare **search** and **length** first. – Sphinx Mar 26 '18 at 16:08
  • @zfrisch No, TO isn't. `search` is _only_ undefined -> [`var` hoisting](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var#var_hoisting) – Andreas Mar 26 '18 at 16:09
  • 2
    It is always awkward to lick my screen in public. These new HTML events are getting crazy! – epascarello Mar 26 '18 at 16:09
  • @Andreas You're right. since ES6 I've been operating under the assumption that var hoisting had been completely removed but apparently not. The only thing I can say is that while you can do this, it's horrible practice for obvious reasons. – zfrisch Mar 26 '18 at 16:16

4 Answers4

0

Try this as your function.

function textOccurrences() {
  var inputField1 = (document.getElementById("inputField1").value);
  var inputField2 = (document.getElementById("inputField2").value);
  var count = inputField1.split(inputField2).length - 1;
  document.getElementId("answer").value = inputField2 + "Occurs" + count + "times";
}
Bibek Shah
  • 419
  • 4
  • 19
0

You can use a regular expression:

function textOccurrences() {
  var inputField1 = (document.getElementById("inputField1").value);
  var inputField2 = (document.getElementById("inputField2").value);
  var re = new RegExp(inputField2, "g");
  var count = inputField1.match(re).length;
  document.getElementById("answer").value = inputField2 + " Occurs " + count + " times";
}

Note that in your code, you have said getElementId instead of getElementById, also contributing to the error.

treyhakanson
  • 4,611
  • 2
  • 16
  • 33
0
function textOccurrences() {
  var inputField1 = (document.getElementById("inputField1").value);
  var inputField2 = (document.getElementById("inputField2").value);
  var reg = new RegExp(inputField2,'g');
  document.getElementById("answer").value = inputField2 + "Occurs" + inputField1.match(reg).length + 
  "times";
}

You can try something like this

0

you can use regex to match any occurence of you chosen letter and then count those matches and you have to syntax errors in your html and javascript
-it is onclick NOT onlick
-and it is getElementById NOT getElementId

here is you code after editing
Javascript

function textOccurrences() {
   var inputField1 = (document.getElementById("inputField1").value);
   var inputField2 = (document.getElementById("inputField2").value);
   console.log("hekasdlkjasd");
   var re = new RegExp(inputField2, "g");
   var count = inputField1.match(re).length;
   document.getElementById("answer").value = inputField2 + " Occurs " + count + " times";

}

Html

 <!DOCTYPE html>
 <html>
    <head>
        <title> Text Occurrences </title>
        <h1> Number of Character Occurrences</h1>
        <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    </head>
 <body>
   <form>
       <p>Enter text: </p>
       <p> <input id="inputField1" type="text" /> </p>

       <p>Enter any character:</p>
       <p> <input id="inputField2" type="text" /> </p>
       <input type="button" value="Search" onclick="textOccurrences();" />
       </p>
       <p>Number of Occurrences:</p>
       <p><textarea id="answer"></textarea></p>
   </form>

 </body>
 </html>
aa-Ahmed-aa
  • 363
  • 4
  • 14
  • I sort of understand now.Is there a way to use indexOf at all? – Manny0 Mar 26 '18 at 16:41
  • indexOf returns the index of the first occurrence of the letter for example `"asdasd".indexOf('d')` will return 2 the first occurrence of d is at position 2 – aa-Ahmed-aa Mar 26 '18 at 16:44