Help with Functions

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.



 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Closed Thread
 
LinkBack Thread Tools
  #1  
Old Jun 9th, 2004, 16:21
New Member
Join Date: Jun 2004
Location: United Kingdom
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!

  #2  
Old Jun 9th, 2004, 16:32
Most Reputable Member
Join Date: Jul 2003
Posts: 1,856
Thanks: 0
Thanked 0 Times in 0 Posts
Sounds like homework to me...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #3  
Old Jun 9th, 2004, 16:40
New Member
Join Date: Jun 2004
Location: United Kingdom
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #4  
Old Jun 9th, 2004, 21:24
Most Reputable Member
Join Date: Jul 2003
Posts: 1,856
Thanks: 0
Thanked 0 Times in 0 Posts
looks close. you need to += the resultString to build it up, otherwise you're going to get just the last letter.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #5  
Old Jun 9th, 2004, 21:48
New Member
Join Date: Jun 2004
Location: United Kingdom
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #6  
Old Jun 10th, 2004, 00:37
Most Reputable Member
Join Date: Jul 2003
Posts: 1,856
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #7  
Old Jun 10th, 2004, 13:04
benbacardi's Avatar
Highly Reputable Member
Join Date: Feb 2004
Location: United Kingdom
Age: 20
Posts: 611
Thanks: 0
Thanked 0 Times in 0 Posts
i cant see that function working anyway.... you've got

if (aString.charAt(position) != isVowel(aCharacter))

but you haven't defined aCharacter, so you would get an error anyway surely?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #8  
Old Jun 10th, 2004, 13:12
benbacardi's Avatar
Highly Reputable Member
Join Date: Feb 2004
Location: United Kingdom
Age: 20
Posts: 611
Thanks: 0
Thanked 0 Times in 0 Posts
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;

}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #9  
Old Jun 10th, 2004, 16:06
New Member
Join Date: Jun 2004
Location: United Kingdom
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #10  
Old Jun 10th, 2004, 17:16
benbacardi's Avatar
Highly Reputable Member
Join Date: Feb 2004
Location: United Kingdom
Age: 20
Posts: 611
Thanks: 0
Thanked 0 Times in 0 Posts
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!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #11  
Old Jun 10th, 2004, 22:28
New Member
Join Date: Jun 2004
Location: United Kingdom
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #12  
Old Jun 11th, 2004, 09:56
benbacardi's Avatar
Highly Reputable Member
Join Date: Feb 2004
Location: United Kingdom
Age: 20
Posts: 611
Thanks: 0
Thanked 0 Times in 0 Posts
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #13  
Old Jun 11th, 2004, 09:58
benbacardi's Avatar
Highly Reputable Member
Join Date: Feb 2004
Location: United Kingdom
Age: 20
Posts: 611
Thanks: 0
Thanked 0 Times in 0 Posts
yup this is all you need:

answer = dropVowels('Trying to drop vowels from this string');

document.write(answer);
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #14  
Old Jun 11th, 2004, 10:09
New Member
Join Date: Jun 2004
Location: United Kingdom
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Ta very much!!!

Simple when it smacks you in the face.

Regards
STaLLiON
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Closed Thread

Tags
help, functions

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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


All times are GMT. The time now is 23:37.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42