Jump to content

Trying to count words in string of unspecified length - javascript

Hey forum, i am currently trying to take some input consisting of an unspecified amount of words,

then count them and return how many times each word is used.. but i have been running into

some issues... 

 

post-955-0-49627800-1443710928.png

This is my return, it's kindof correct. Just redundant... i want to prevent it from counting and returning

the same word several times. Desired output would be "er:3 det:2 da:1"

 

post-955-0-83641600-1443710996.png

This is where i am getting my values and inserting the text in html. 

 

post-955-0-37968300-1443711001.png

 

Here is my loop. I make an array based on the input text. As you can hopefully see. My loop recounts,

and returns the same word all over again whenever it encounters it ....  i haven't been able to prevent

that and i'm wondering if you have any suggestions... among various solutions i tried another nested

loop based on x doing txtarray.splice(x, txtarray) to remove a word after i have counted it once. But

that didn't work at all... it just ran the loop without doing anything.

 

i also tried setting a variable in the first loop "var check = txtarray" then doing txtarray.splice(x, check),

but again.. nothing happened. 

Link to comment
Share on other sites

Link to post
Share on other sites

When you go to append the text to the HTML document, first check if count is greater than 1, if it is, don't append it.

 

I'm not that good with javascript, so I don't know how you would code that.

Link to comment
Share on other sites

Link to post
Share on other sites

When you go to append the text to the HTML document, first check if count is greater than 1, if it is, don't append it.

 

I'm not that good with javascript, so I don't know how you would code that.

that isn't going to help though, count is checking how many times the loop encounters a specific word 

in the array. If it doesn't return a count grater than 1, that means whenever it counts a word twice or more

there is no output for that word.

 

I still need to count all the words, and return x amount it encounters said word. But i want to prevent it from 

returning the same word with the same count whenever it sees it, just return the word and how many times

it was encountered once.

Link to comment
Share on other sites

Link to post
Share on other sites

That was my bad, I looked through the code some more and I would suggest using the indexOf function

http://www.w3schools.com/jsref/jsref_indexof.asp

 

you would basically check using the indexof function if the word exists already

 

Basically you would create a temporary string that you would append to after you check. When you go to add to the string, use the following code

if(str.indexOf(txtarray === -1)

{

str = str + check + ": " + count;

}

 

Then just do:

settxt.innerHTML = str;

 

Hopefully that makes sense, I'm not good at explaining things

Link to comment
Share on other sites

Link to post
Share on other sites

Yes, that does indeed seem very sensible. Thanks, i will give it a try  :D

i'm still very curious as to how i should to solve my nested loop pitfall though,

to be able to handle them better, it's an issue i have encountered more than

once. without using indexof that is.

Link to comment
Share on other sites

Link to post
Share on other sites

  • 2 weeks later...

Btw, if anyone are interested. My initial solution would have worked, it's just that i was using splice wrong,

hence why it didn't do anything. 

it's splice(start/value, how many)  so array.splice(i, 1)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×