0

i'm trying to write a function which counts the number of syllables in a piece of text, i know it won't be that accurate, it's just a simple version I need at the moment which roughly counts the number of syllables.

whenkeydown = function(max_length) {
$("#my_text").unbind().keyup(function() {
    //check if the appropriate text area is being typed into
    if (document.activeElement.id === "my_text") {
        //get the data in the field
        var text = $(this).val();

        function countSyllables(words) {
            console.log(words);
            for (var z; z < words.length; z++) {
                word = word[z].toLowerCase();                                     
                if(word.length <= 3) { return 1; }                             
                word = word.replace(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/, '');   
                word = word.replace(/^y/, '');                                 
                syllables = word.match(/[aeiouy]{1,2}/g).length; 
                syllableCount += syllabes   
                }              
        return syllableCount
        }

        var syllableCount = countSyllables(text)

        $("#noOfSyllables").html("").html(syllableCount).css("color", "#F7860C"); //orange

The error i get in the (aptana/firefox) debugger is "TypeError: $(...).html(...).html(...).css is not a function", does this mean my function does not return anything?

4
  • 2
    Setting the html() to nothing and than to another value is a useless step. There is no reason to blank it out. Commented Nov 26, 2012 at 13:53
  • 1
    Are you sure that you are linked the jquery library source? Commented Nov 26, 2012 at 13:55
  • 1
    @sємsєм yes i have, <script src="ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" type="text/javascript"></script> Syllable Count: <em><span id="noOfSyllables"></span></em> in my html file and Commented Nov 26, 2012 at 14:12
  • @epascarello oh yes, that was copied from another line where i change the colour of the text based on how many characters are there, for which i need to set it to nothing, but yes you're right, it is unnecessary on this line Commented Nov 26, 2012 at 14:13

1 Answer 1

3

Your variable syllableCount is never set in your function countSyllables.

I expect to see a var syllableCount = 0;

function countSyllables(words) {
    var syllableCount = 0;
    ...

second issue.

You are treating text() as if it returns an Array. It is a single string.

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

2 Comments

oh yes, that was stupid of me, that fixes the error, although i still get a value returned of zero regardless
Because it looks like words is supposed to be an Array and it is a single string. :)

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.