This is a discussion on "Help with Functions" within the JavaScript Forum section. This forum, and the thread "Help with Functions are both part of the Program Your Website category.
|
|
|
|
|
![]() |
||
Help with Functions
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
|||
|
Help with Functions
I have created a library called stringLibrary.js with a function called 'isVowel' as shown below
function isVowel(aCharacter) /************************************************** **********************/ /*Function takes one argument, a string which consists of a single */ /*character. The function returns the boleen value true if the character*/ /*is one of a, A, e, E, i, I, o, O, u, U. If the character is not one of*/ /*these the function returns the bolean value false. */ /************************************************** **********************/ { return ((aCharacter == 'a') || (aCharacter == 'A') || (aCharacter == 'e') || (aCharacter == 'E') || (aCharacter == 'i') || (aCharacter == 'I') || (aCharacter == 'o') || (aCharacter == 'O') || (aCharacter == 'u') || (aCharacter == 'U')) }; I now want to create another function called 'dropVowels'. This function needs to take a string as its argument and return a new string with vowels removed, Whilst using the 'dropVowel function??? I am a newbie to Javascript so any help would be great. Thanks |
|
|
|
|||
|
Sounds like homework to me...
|
|
|||
|
Yeah!! howd u tell!!!
Strugling withit though, Thought maybe reverse engineering would help me understand it better. This is my idea of the dropVowel code. function dropVowels(aString) /************************************************** **********************/ /*Function takes one argument, a string. */ /*Function returns a new string with vowels removed. */ /************************************************** **********************/ { var resultString; resultString = ''; for (var position = 0; position < aString.length; position = position + 1) { if (aString.charAt(position) != isVowel(aCharacter)) { resultString = aString.charAt(position) } }; return resultString }; Reagrds STaLLiON |
|
|||
|
looks close. you need to += the resultString to build it up, otherwise you're going to get just the last letter.
|
|
|||
|
So if i ammend to this:-
function dropVowels(aString) /************************************************** **********************/ /*Function takes one argument, a string. */ /*Function returns a new string with vowels removed. */ /************************************************** **********************/ { var resultString; resultString = ''; for (var position = 0; position < aString.length; position = position + 1) { if (aString.charAt(position) != isVowel(aCharacter)) { resultString += aString.charAt(position) } }; return resultString }; I get a Error:'Length' is null or not a object, when i try to call in the function onto a web page. In the If statement have i called in the function isVowel in the correct way? I have a stringLibrary.js file with these 2 functions inside plus more and i am not sure how to call in functions within functions!!! Thanks for helping Regards STaLLiON |
|
|||
|
At a guess, your function may not know the value being passed is a string, so the string object methods and properties aren't available. I'd try something like
aString = String(aString); near the beginning of the function to cast it to a string object explicitly. |
|
||||
|
ok i've just been playing around with it and this is what i've come up with... it seems to work here:
function dropVowels(aString) { var resultString; aString = String(aString); resultString = ''; for (var position = 0; position < aString.length; position = position + 1) { character = aString.charAt(position); yesno = isVowel(character); if (yesno == false) { resultString += character; } } return resultString; } |
|
|||
|
Cheers
Seems to have done the job!!! Just need to understand how it works now, sure it will come to me. Ta very much Regards STaLLiON |
|
||||
|
basically, the "character = aString.charAt(position)" asigns the current letter to the variable "character". "yesno" then checks using the isVowel function to see if the variable "character" is a vowel - obviously if it is then it returns true, if not, returns false. The if statement says "if yesno equals false, add that character to the end of "resultString". Then you know the rest!
|
|
|||
|
Ok, Think i understand that!!!
So next step is to make use of the Function from my web page, Here is what i came up with:- <SCRIPT SRC = stringLibrary.js> </SCRIPT> <SCRIPT> dropVowels('Trying to drop vowels from this string'); var answer; answer = resultString; document.write(answer); All i get is Error: resultString is undefined, Y??? Regards STaLLiON PS:- I am new to this by the way |
|
||||
|
my guess is that you've used return in the function dropVowels, but you havent returned it to anything. im guessing (lol) that you need to add answer = dropVowels('Trying.. instead of
dropVowels('Trying to drop vowels from this string'); var answer; answer = resultString; ill have another play and see |
![]() |
| Tags |
| help, functions |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Functions? | Jack Franklin | PHP Forum | 10 | Feb 3rd, 2008 16:49 |
| Functions and conditional IFs | AdRock | PHP Forum | 4 | Sep 17th, 2007 08:18 |
| Calling functions | thriftyspider | Classic ASP | 5 | Aug 7th, 2007 14:54 |
| Running Php functions | tomd1985 | PHP Forum | 1 | Feb 11th, 2006 19:33 |
| Need help with webpage functions | lordfinesse | Web Page Design | 0 | Jul 26th, 2005 02:50 |