1

I'm working on a project that requires me to return the Flesch Readability Index for text entered in an input box. The formula is as follows:

206.835-85.6(number of syllables / number of words)-1.015(number of words / number of sentences)

I've figured out how to count the words, and pretty sure I know how count the sentences, but I have no idea how to go about counting the syllables.

Here's the information I was given on what to count as a syllable:

Each group of adjacent vowels (a, e, i, o, u, y) counts as one syllable.
Each word has at least one syllable even if the above rule gives it a count of 0.

Would anyone be willing to point me in the right direction on how to go about this?

Update I have written some code but am having trouble getting it to work. I posted a link to a jsfiddle I'm working on as a comment to the answer below along with the issue I'm having. If anyone would be willing to look it over and see if you can help me figure out what I'm doing wrong (other than that there is most likely a more efficient way of doing this), it would be much appreciated.

1 Answers1

2

pseudocode:

for each letter in word:
    if letter is vowel, and previous letter is not vowel or this is the first letter, increment numSyllables

if numSyllables is 0, set numSyllables to 1

You can use a boolean as a flag to determine whether the previous letter was a vowel or not.

To loop through each letter in a word:

var word = "test",
    i;
for(i = 0; i < word.length; i++) {
    console.log(word[i]);
}
forgivenson
  • 4,394
  • 2
  • 19
  • 28
  • Thanks! I guess one of the main things throwing me off is how to get it to read each letter at a time, if that makes sense. Like, I understand how your pseudocode works, but like on "for each letter in word", not sure how to do the each letter part. Maybe that's at least kind of clear ha. – Beth Tanner Dec 12 '14 at 20:29
  • In JavaScript, you can index into a string just like an array. I'll add an example to my answer. – forgivenson Dec 12 '14 at 20:41
  • Having some trouble getting this to work. When I try to test it in my web browser, the page locks up and I get an "unresponsive script" error that when running the debugger seems to be occurring due to my wicked long if statement. I have very limited knowledge on using the browser's debugger so am not really sure what it is that's throwing this off. Here is a link to a jsfiddle that I'm working with, if you don't mind, do you think you could take a look? http://jsfiddle.net/nreutjd3/ I'm sure there's a more efficient way to write this, but wasn't really sure how to go about it. – Beth Tanner Dec 13 '14 at 07:55
  • I noticed some typos that had slipped through the error catcher on the jsfiddle and corrected them, however, I'm still getting the same result. Here is the updated fiddle link http://jsfiddle.net/nreutjd3/1/ – Beth Tanner Dec 13 '14 at 08:18