Needing function help

This is a discussion on "Needing function help" within the JavaScript Forum section. This forum, and the thread "Needing function help 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 Jul 3rd, 2004, 16:20
New Member
Join Date: Jul 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Needing function help

I'm really needing some help please. I notice that the problem I have has been asked by someone else but it still won't work for me. I have one function as below and need to create a new function that drops vowels from a string and uses the isVowel function. I need to use the following plan:-

First declare a local variable called resultString and assign to it an empty string.
Next write code to iterate over the argument string, using a for
loop to access each character of the string in turn.
You will need to use an if statement within the for loop to check
whether the current character is a vowel or not (using the function
isVowel() ). If the current character is not a vowel then add it to
the variable resultString .
Finally, return resultString as the result of your function.

function isVowel(aCharacter)
/************************************************** ******/
/* This function takes one argument, a string which */
/* consists of a single character. The function */
/* returns the Boolean value true if the character is */
/* one of a, A, e, E, i, I, o, O, u, or U. If the */
/* character is not one of these the function returns */
/* the Boolean value false */
/************************************************** ******/
{
return ((aCharacter == 'a') ||
(aCharacter == 'A') ||
(aCharacter == 'e') ||
(aCharacter == 'E') ||
(aCharacter == 'i') ||
(aCharacter == 'I') ||
(aCharacter == 'o') ||
(aCharacter == 'O') ||
(aCharacter == 'u') ||
(aCharacter == 'U'))
}
This is what I have so far
function dropVowels(resultString)
/************************************************** *****/
/* This function takes a string as its argument and */
/* returns, as its result, a new string similar to the */
/* argument except that the vowels have been removed */
/************************************************** *****/

{
var resultString; //local variable to hold the new string
resultString = '';

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

Sorry this post is so long
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!

  #2  
Old Jul 3rd, 2004, 16:24
New Member
Join Date: Jul 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
I forgot to say that I also need to write a program using this function using the following:-

Call the function dropVowels() with the argument 'M150 is a
first level course' and assign the result to a variable called
answer.
Display the string referenced by the variable answer in an alert()
dialogue box.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #3  
Old Jul 3rd, 2004, 19:03
Most Reputable Member
Join Date: Jul 2003
Posts: 1,856
Thanks: 0
Thanked 0 Times in 0 Posts
As an Admin for this section I'm going to lay down the following rule regarding homework related questions.

If you need help with your homework, ask only specific questions about your problem. If it's not obvious that you're trying to do it yourself and that you're just stuck with something then you won't get an answer.

For those who want to help. Please put your answers in a way that leads the student to solving the problem on their own, like the syntax for blah is blah or always put a ; on the end of each statement. Please do not write their code for them.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #4  
Old Jul 3rd, 2004, 19:04
Most Reputable Member
Join Date: Jul 2003
Posts: 1,856
Thanks: 0
Thanked 0 Times in 0 Posts
The previous reply wasn't specific to you stealthy1, just something general.

So, what problem are you having with your solution?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #5  
Old Jul 3rd, 2004, 20:09
New Member
Join Date: Jul 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
I seem to be having a problem calling the function which makes me think that the code that I have for the function could be wrong. I am new to this and admit to finding it quite hard at times.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #6  
Old Jul 3rd, 2004, 20:56
Most Reputable Member
Join Date: Jul 2003
Posts: 1,856
Thanks: 0
Thanked 0 Times in 0 Posts
What kind of error are you getting and how are you trying to call the function?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #7  
Old Jul 3rd, 2004, 23:31
New Member
Join Date: Jul 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
I'm getting just a blank screen. This is what I have got so far

function dropVowels(resultString)
{
var answer;
answer = dropVowels('This is a test');

document.write(answer);
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #8  
Old Jul 4th, 2004, 02:04
Most Reputable Member
Join Date: Jul 2003
Posts: 1,856
Thanks: 0
Thanked 0 Times in 0 Posts
Well, the function call looks ok. What's your dropVowels function look like?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #9  
Old Jul 4th, 2004, 09:26
New Member
Join Date: Jul 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Ok, I have

function dropVowels(resultString)
/************************************************** *****/
/* This function takes a string as its argument and */
/* returns, as its result, a new string similar to the */
/* argument except that the vowels have been removed */
/************************************************** *****/

{
var resultString; //local variable to hold the new string
resultString = '';

for (var resultString = 0; string < resultString.length; string++)
{
if (!isVowel(resultString.charAt(string)))
{
resultString = resultString + 1;
return resultString;
}
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #10  
Old Jul 4th, 2004, 17:48
Up'n'Coming Member
Join Date: Oct 2003
Posts: 69
Thanks: 0
Thanked 0 Times in 0 Posts
You have two different functions with the same name.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #11  
Old Jul 4th, 2004, 17:57
New Member
Join Date: Jul 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Sorry, being a bit thick here but what do you mean?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #12  
Old Jul 4th, 2004, 19:56
Most Reputable Member
Join Date: Jul 2003
Posts: 1,856
Thanks: 0
Thanked 0 Times in 0 Posts
var resultString; //local variable to hold the new string
resultString = '';
...for your result

and

var resultString = 0;
...for your loop counter

Two different things with the same name so they're overwriting each other.

Aside from that you probably don't want to build up your result string by adding + 1 to it each time a non-vowel is found. Then all you'll get is the number of non-vowels. You probably want to add the currently tested characted to the your resultString instead.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #13  
Old Jul 4th, 2004, 21:35
New Member
Join Date: Jul 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Ok, if I try this

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

for (var count = 0; count < aString.length; count = count + 1)
{
resultString = aString.charAt(count);
if (!aVowel(aString.charAt(count)))
{
result = result + resultString;
}
}
resultString = result
return resultString;
};
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #14  
Old Jul 4th, 2004, 22:51
Most Reputable Member
Join Date: Jul 2003
Posts: 1,856
Thanks: 0
Thanked 0 Times in 0 Posts
This is redundant:

resultString = result
return resultString;

Why not just do?

return result;

And if you define:
resultString = aString.charAt(count);
then you might as well use the variable in the next line:
if (!aVowel(aString.charAt(count)))

But, those are just about writing better code. They shouldn't affect how it works. Does what you wrote work ok?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #15  
Old Jul 5th, 2004, 04:47
Up'n'Coming Member
Join Date: Oct 2003
Posts: 69
Thanks: 0
Thanked 0 Times in 0 Posts
You have this code:
Code: Select all
function dropVowels(resultString)
{
var answer;
answer = dropVowels('This is a test');

document.write(answer);
}
And this code:
Code: Select all
function dropVowels(resultString)
/*******************************************************/
/* This function takes a string as its argument and */
/* returns, as its result, a new string similar to the */
/* argument except that the vowels have been removed */
/*******************************************************/

{
var resultString; //local variable to hold the new string
resultString = '';

for (var resultString = 0; string < resultString.length; string++)
{
if (!isVowel(resultString.charAt(string))) 
{
resultString = resultString + 1;
return resultString;
}
}
Two different functions with the same name.

I think it might be best if you post your whole code...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #16  
Old Jul 5th, 2004, 10:52
New Member
Join Date: Jul 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
I have the function isVowel in a function library and need to create a new function called dropVowels and place this in the function library. I then need to call the isVowel function to create an alert box with a string with the vowels missing. I'm very confused!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #17  
Old Jul 5th, 2004, 16:44
Up'n'Coming Member
Join Date: Oct 2003
Posts: 69
Thanks: 0
Thanked 0 Times in 0 Posts
You still have two functions with the same name...

And in one of the functions you set the incoming argument to nothing.... so how do you expect to pull vowels for an empty variable?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #18  
Old Jul 5th, 2004, 19:35
New Member
Join Date: Jul 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks guys for your help. I have now got it to work much to my delight. Sheer stupidity and simple errors were the problem!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #19  
Old Jul 5th, 2004, 21:51
Most Reputable Member
Join Date: Jul 2003
Posts: 1,856
Thanks: 0
Thanked 0 Times in 0 Posts
It's the simple things that get you.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #20  
Old Jul 6th, 2004, 15:13
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
it was the same with the other guy who posted EXACTLY the same problem...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Closed Thread

Tags
needing, function, help

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
desperatly needing help with css OMorchoe Web Page Design 4 May 10th, 2008 08:52
Newbie needing template mr2nut Starting Out 5 Aug 10th, 2007 13:29
Needing Some Help 4WardMotion Starting Out 5 Dec 22nd, 2006 03:37