SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jun 2004
    Location
    Runcorn
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I A Function call not working as Expected?

    Hi All,

    I am trying to invoke a function using a FORM Alert box but I am getting the wrong result. The 'Alert' box only displays the first 'H' of the string ' Hello There' when I all of the 'string' shoud be displayed excluding the the vowels...

    Can anybody give me a hint as to what is wrong with the coding?

    I have included the 'Coding' below. I apologise for the coding not being indented, but when copied it automatically justfies to the left.

    <HTML>
    <HEAD>
    <TITLE> Function Prog</TITLE>
    <SCRIPT>

    function aVowel(aCharacter)
    {
    return ((aCharacter == 'a') || (aCharacter == 'A') ||
    (aCharacter == 'e') || (aCharacter == 'E') ||
    (aCharacter == 'i') || (aCharacter == 'I') ||
    (aCharacter == 'o') || (aCharacter == 'O') ||
    (aCharacter == 'u') || (aCharacter == 'U'))
    };


    function loseVowels(aString)
    /********************************************************/
    /*This function takes a string as its argument and */
    /*returns, as its result, a new string which is similar */
    /*to the argument string except that all the vowels */
    /*have been removed. */
    /********************************************************/
    {
    outPutString = (''); //local variable to hold the new string

    for (var count = 0; count < aString.length; count = count + 1)
    {
    outPutString = aString.charAt(count);
    if (!aVowel(aString.charAt(count)))
    {
    return outPutString;
    }
    }
    };


    /*The code below calls the function 'loseVowels()' */
    /*with an argument which will assign the result to a */
    /*variable which in turn will be displayed in a 'alert' box */

    loseVowels('Hello There')

    joker = outPutString

    /* Code to create a form with a button which produces an alert box */

    </SCRIPT>
    </HEAD>
    <BODY>

    <FORM NAME = "Joker">

    <INPUT TYPE = "button"
    VALUE = "Press Me"
    ONCLICK = "window.alert(joker);">

    </FORM>
    </BODY>
    </HTML>

    Regards
    Gerrw

  2. #2
    JavaScript Guru (Big Ego) Arielladog's Avatar
    Join Date
    Jul 1999
    Location
    SC, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    I think the problem is with this:

    function loseVowels(aString)
    /********************************************************/
    /*This function takes a string as its argument and */
    /*returns, as its result, a new string which is similar */
    /*to the argument string except that all the vowels */
    /*have been removed. */
    /********************************************************/
    {
    outPutString = (''); //local variable to hold the new string

    for (var count = 0; count < aString.length; count = count + 1)
    {
    outPutString = aString.charAt(count);
    if (!aVowel(aString.charAt(count)))
    {
    return outPutString;
    }
    }

    };


    What you want to do is loop through each character, if it's not a vowel add it to the output (don't return the output). For exmaple, in the first iteration of the loop, outPutString = 'H'

    It's not a vowel, so it goes down and "return outPutString"

    So in this way, outPutString (which is 'H') is returned.

    Note that the function you want is here:

    http://www.sitepoint.com/article/ori...rogramming-2/2

    Also note that a switch() statement is probably better inthis case than an if():

    function aVowel(aCharacter)
    {
    switch(aCharacter.toLowerCase()){
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
    return true;
    default:
    return false;
    }};

    aDog

  3. #3
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    > Note that the function you want is here:
    > http://www.sitepoint.com/article/or...programming-2/2

    No the best one IMHO. In Javascript there is no need of pascal-like string loops, since we can always use the force of regexps.

    Compare above code with this

    Code:
    joker = 'Hello There'.replace(/[AEIOUY]/gi, ''); // that's all...
    Worth to read: Introduction to Regular Expressions @ microsoft

    http://msdn.microsoft.com/library/en...xpressions.asp


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •