previous page

Overview | Part B Folder | newStory function | User Input | Build Story | Image | Gender | Caps Function | A/An Function | Image Rollover | Story Window | Questions

next page
JavaScript Storyteller, Part B

Build a/an function


 

Sometimes the user’s input requires adjusting some words and capitalization. We’ve underlined an example where the article is a before a word beginning with a vowel (aeiou) and an before a word beginning with a consonant (all other letters):

Linda was in her kitchen, busy making a pineapple pie. Pineapple was her favorite fruit, but this pie was for her friend Ralph. She was almost done when her pet Peruvian pygmy rhinoceros Edward James Olmos leaped onto the kitchen counter...

Right now a works with pineapple, but what if the user selected apple pie? Then we would want the story to read "busy making an apple pie."

  1. We'll call this function the a_or_an function and place it immediately after the end of the caps function:

    function a_or_an(word)
    {

    /* basic strategy: make character lower case since "aeiou" are all lower case, then see if the character is in the string "aeiou"; indexes for vowels are 0, 1, 2, 3, and 4n.
    indexOf returns the location of the character in a string; if it's not found it returns -1 */

        //grabs first letter of word (like caps function)
        var firstLetter = word.charAt(0);

    //create list of vowels
    var vowels = "aeiou";
    if (vowels.indexOf(firstLetter.toLowerCase()) < 0)
    {
        /* indexOf returns -1 if char not found in string */
        return 'a ' + word;
    }
    else
    {
        return 'an ' + word;
    }

    }


  2. Find words in your story that are supplied by the user and preceded by a or an. If you don't have any, change your story slightly to include at least one situation where this function will be needed.

  3. Call the function from the story and pass the word to the function just like you did with the caps function.

  4. Save the file and preview in Firefox. Fix any errors with the Error Console. Does everything work now? Do the words in your story look right? Did you miss any?


    5. Go to the Catalyst WebQ and answer Questions 15 and 16.

    Now that you have fixed the grammar problems, add your rollover image....

previous page   next page