I am making a character counter as a hobby code. So far, it is working, but I have a glitch I can't seem to solve. When I write the terms "a,b,c
", then it correctly writes a:1 b:1 c:1
. But when I write "a,a,c,c
", then it only writes a:2
. I am not sure what's wrong. Here is the JavaScript portion of my code (myFunction
is activated by a button, and testinput.value
is the value of the textbox I am using):
function myFunction() {
var occurence = document.getElementById("testinput").value;
var cycleOne = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
"u", "v", "w", "x", "y", "z"
];
for (i = 0; i < 26; i++) {
var counter = new RegExp(cycleOne[i], 'g');
document.write(occurence.match(counter).length);
}
}