1

Hi guys I am really stuck in this one situation :S I have a local .txtfile with a random sentence and my program is meant to :

I am finding it difficult to execute the third question. My code is ..

JavaScript

      lengths.forEach((leng) => {
      counter[leng] = counter[leng] || 0;
      counter[leng]++;

});     
      $("#display_File_most").text(counter);

     }


    }          
        r.readAsText(f);
    }
});

  </script> 

I have used this question for help but no luck - Using Javascript to find most common words in string?

I believe I have to store the sentence in an array and loop through it, uncertain if that is the correct step or if there is quicker way of finding the solution so I ask you guys.

Thanks for your time & I hope my question made sense :)

Community
  • 1
  • 1
Sam
  • 15
  • 1
  • 4

2 Answers2

1

3.Produce a list of number of words of each length in sentence (not done).

Based on the question would this not be the solution?

var words = str.split(" ");
var count = {};

for (var i = 0; i<words.length; i++){
 count[words[i].length] = (count [words[i].length] || 0) + 1
}
Kamal
  • 161
  • 5
  • 1
    Good answer, but I think it should be `count = []` (although `{}` works too). You want to create an array and not an object, you access with an index not a string. – maraca Mar 15 '17 at 01:47
  • that's true. I was initially thinking words to be keys. But this was simpler. – Kamal Mar 15 '17 at 14:49
1

If you think of your solution as separated well done tasks, it would be really simple to find it. Here you have them together:

  1. Convert the words into an array. Your guts were right about this :)

    var source = "Hello world & good morning. The date is 18/09/2018";
    var words = source.split(' ');
    
  2. The next step is to find out the length of each word

    var lengths = words.map(function(word) { 
      return word.length; 
    });
    
  3. Finally the most complicated part is to get the number of occurrences for each length. One idea is to use an object to use key/value where key is the length and value is its count (source: https://stackoverflow.com/a/10541220/1505348)

Now you will see under the counter object have each word length with its repetition number on the source string.

var source = "Hello world & good morning. The date is 18/09/2018";
var words = source.split(' ');

var lengths = words.map(function(word) { 
  return word.length; 
});

var counter = {};
lengths.forEach((leng) => {
  counter[leng] = counter[leng] || 0;
  counter[leng]++;
});

console.log(counter);
Community
  • 1
  • 1
Lucio
  • 4,753
  • 3
  • 48
  • 77
  • Hi Lucio, awesome job and everything does seem to be working (the word length) just having trouble displaying it right now it shows - [object Object] my code is updated to reflect this ... sorry if its a silly question.. its kind of late where I am Zzz – Sam Mar 15 '17 at 01:32
  • 1
    @Sam You can use `arr.toString()` or `arr.join(', ')` or `JSON.stringify(arr)` (the last one has the braces `[]` already included). – maraca Mar 15 '17 at 01:55