0

I'm trying to write a method that takes the words in a sentence, lists the different words, and the number of times they occur, but I can't get it to work.

This is what I have so far:

var wordsArr = ['one', 'fish', 'two', 'fish', 'red', 'fish', 'blue', 'fish']
var wordsObj = {};
for (var i = 0; i < wordsArr.length; i++) {
   var counter = 0;
   wordsObj.wordArr[i] = counter++;
}
return wordsObj;

At the end of it I'm hoping to see this returned:

{ one : 1, fish : 4, two : 1, red : 1, blue : 1 }

My console is telling me,

'Uncaught TypeError: Cannot set property '0' of undefined'

I assume undefined is referring to the counter variable. I've tried declaring this both within and before the loop (as 0), but this doesn't work.

Is it something to do with wordsArr[i] being returned as a string and therefore not being set properly as a property of wordsObj?

If anyone can tell me where I'm going wrong I'd appreciate it. Thanks in advance.

2 Answers 2

1

the property will keep track of the counted instances so you don't need a counter variable:

var wordsArr = ['one', 'fish', 'two', 'fish', 'red', 'fish', 'blue', 'fish']
var wordsObj = {};
for (var i = 0; i < wordsArr.length; i++) {
   //var counter = 0;
   wordsObj[wordsArr[i]] = ( wordsObj[wordsArr[i]] || 0 ) + 1;
}
console.log( wordsObj );

apart form that you had some minor syntax errors ...

Sign up to request clarification or add additional context in comments.

2 Comments

Just what I was after. Didn't occur to use bracket notation to add the property, being an indexed array element, or to simply use the property to return the existing counter value... Thanks!
glad that helped you!
0

One other way of doing this could be

var longText = "These excellant intentions were strengthed when he enterd the Father Superior's diniing-room, though, stricttly speakin, it was not a dining-room, for the Father Superior had only two rooms alltogether; they were, however, much larger and more comfortable than Father Zossima's. But tehre was was no great luxury about the furnishng of these rooms eithar. The furniture was of mohogany, covered with leather, in the old-fashionned style of 1820 the floor was not even stained, but evreything was shining with cleanlyness, and there were many chioce flowers in the windows; the most sumptuous thing in the room at the moment was, of course, the beatifuly decorated table. The cloth was clean, the service shone; there were three kinds of well-baked bread, two bottles of wine, two of excellent mead, and a large glass jug of kvas -- both the latter made in the monastery, and famous in the neigborhood. There was no vodka. Rakitin related afterwards that there were five dishes: fish-suop made of sterlets, served with little fish paties; then boiled fish served in a spesial way; then salmon cutlets, ice pudding and compote, and finally, blanc-mange. Rakitin found out about all these good things, for he could not resist peeping into the kitchen, where he already had a footing. He had a footting everywhere, and got informaiton about everything. He was of an uneasy and envious temper. He was well aware of his own considerable abilities, and nervously exaggerated them in his self-conceit. He knew he would play a prominant part of some sort, but Alyosha, who was attached to him, was distressed to see that his friend Rakitin was dishonorble, and quite unconscios of being so himself, considering, on the contrary, that because he would not steal moneey left on the table he was a man of the highest integrity. Neither Alyosha nor anyone else could have infleunced him in that.",
       words = longText.split(" ").map(w => w.toLowerCase()),
   wordCount = words.reduce((p,c) => (p[c] === void 0 ? p[c] = 1 : ++p[c],p),{});
console.log(wordCount);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.