/    Sign up×
Community /Pin to ProfileBookmark

Help with a Function

Hi Guys,

I am trying to design a function that will strip out all the vowels from a string and I cannot get this part right for the life of me! Please help.

Here is my first function which tells us if a character is a vowel and it works fine, now I am trying to use this function within the code below it but I am not sure if my arguments are right.

function isVowel(aCharacter)
/************************************************************************/
/*
a code fragment for TMA03 Q4(i) /
/* a function to check whether a given character is a vowel /
/*
**
*********************************************************************/
{
return ((aCharacter == ‘a’) || (aCharacter == ‘A’)||
(aCharacter == ‘e’) || (aCharacter == ‘E’)||
(aCharacter == ‘i’) || (aCharacter == ‘I’)||
(aCharacter == ‘o’) || (aCharacter == ‘O’)||
(aCharacter == ‘u’) || (aCharacter == ‘U’))
};

Here is my problem!

function dropVowels(aString)
/************************************************************************/
/*
/
/* a function to drop the vowels in a string /
/*
**
*********************************************************************/
{
var resultString = ”;

for (var result = 0; result <= aString.length; result = result + 1)
{
if (aString.charAt(result) != isVowel (aString.charAt(result)))
resultString = aString.charAt(result)
};
return resultString

};

Thanks guys

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@VladdyJun 04.2004 — Someone was asking help with the same homework a few weeks ago. Search the forums.
Copy linkTweet thisAlerts:
@KrustyauthorJun 04.2004 — Thanks ?
Copy linkTweet thisAlerts:
@KrustyauthorJun 04.2004 — Hi Guys,

On second thoughts, forget I asked because I think that it would be cheating and I rather fail and learn from my mistakes than to have someone help me and not know why or how they reached that answer.

K
Copy linkTweet thisAlerts:
@CBDSteveJun 04.2004 — Hmmm...

Still, you can replace a character in one line using regular expressions, eg

sMyString.replace(/a/g, "The Letter A")
Copy linkTweet thisAlerts:
@BhanuJun 04.2004 — Try this

Bhanu


--------------------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>Untitled</title>

</head>

<script language="JavaScript">

function isVowel(aCharacter)

{

if ((aCharacter == 'a') || (aCharacter == 'A')||

(aCharacter == 'e') || (aCharacter == 'E')||

(aCharacter == 'i') || (aCharacter == 'I')||

(aCharacter == 'o') || (aCharacter == 'O')||

(aCharacter == 'u') || (aCharacter == 'U')){return true}

else{return false}

}



function dropVowels(aString)

/* /

/*
a function to drop the vowels in a string /

/ **************************************************

*
**
*******************/

{

var resultString = '';

for (var result = 0; result <= aString.length; result = result + 1)

{

if (!isVowel (aString.charAt(result)))

resultString = resultString + aString.charAt(result)

};

document.forms[0].t2.value = resultString;

return resultString;

}

</script>

<body>

<form>

<input type="Text" name="t1" onblur="dropVowels(this.value);">

<input type="Text" name="t2">

</form>

</body>

</html>
---------------------------------------------

Copy linkTweet thisAlerts:
@CBDSteveJun 04.2004 — function dropVowels(_sString)

{

var sReturn = _
sString;

_sReturn = _sReturn.replace(/a/g, "");

_sReturn = _sReturn.replace(/e/g, "");

_sReturn = _sReturn.replace(/i/g, "");

_sReturn = _sReturn.replace(/o/g, "");

_sReturn = _sReturn.replace(/u/g, "");

return sReturn;

}
Copy linkTweet thisAlerts:
@PittimannJun 04.2004 — Hi!

Yet a bit shorter:

str='abcdefghIUv'

str=str.replace(/[aeiouAEIOU]/g,'');

alert(str);

I think, Krusty has taken the right decision:
On second thoughts, forget I asked because I think that it would be cheating and I rather fail and learn from my mistakes than to have someone help me and not know why or how they reached that answer.[/QUOTE]Cheers - Pit
×

Success!

Help @Krusty spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...