Beginner programmer here. I'm struggling with an assignment that's taking input text, splitting the words into single array items and then listing the total number of each words in an output. The splitting of the input works fine, but I need to check the array for duplicate items and remove that item (need to keep it unique) while also increasing the count on that particular word.
The idea was to make an array consisting of the words alone, and another that keeps track of the count. Glad to receive tips to use a simpler approach as well.
And yes, I know there is several solutions to this problem on SO, but I dont quite understand how to fix this particular code using functions.
function gen() { var arr = []; var counter = []; var str = document.getElementById("inpTxt").value; str.toString(); str = str.split(" "); for(var i = 0; i < str.length; i++) { arr.push(str[i]); counter[i]++; //ignore that this array hasnt been properly declared yet, Im trying to make this equal length of arr with default value 0 //tried nested loop here for making comparison, didnt work document.getElementById("print").innerHTML += "Total number of the word \"" + arr[i] + "\": " + counter[i] + " <br />"; } }