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.
|
|
|
|
|
![]() |
||
Needing function help
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
|||
|
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 |
|
|
|
|||
|
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. |
|
|||
|
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. |
|
|||
|
The previous reply wasn't specific to you stealthy1, just something general.
So, what problem are you having with your solution? |
|
|||
|
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.
|
|
|||
|
What kind of error are you getting and how are you trying to call the function?
|
|
|||
|
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); } |
|
|||
|
Well, the function call looks ok. What's your dropVowels function look like?
|
|
|||
|
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; } } |
|
|||
|
You have two different functions with the same name.
|
|
|||
|
Sorry, being a bit thick here but what do you mean?
|
|
|||
|
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. |
|
|||
|
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; }; |
|
|||
|
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? |
|
|||
|
You have this code:
I think it might be best if you post your whole code... |
|
|||
|
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!
|
|
|||
|
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? |
|
|||
|
Thanks guys for your help. I have now got it to work much to my delight. Sheer stupidity and simple errors were the problem!
|
|
|||
|
It's the simple things that get you.
|
![]() |
| Tags |
| needing, function, help |
| Thread Tools | |
|
|
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 help!! | eddie21leeds | Introduce Yourself | 5 | May 6th, 2008 07:41 |
| Newbie needing template | mr2nut | Starting Out | 5 | Aug 10th, 2007 13:29 |
| Needing Some Help | 4WardMotion | Starting Out | 5 | Dec 22nd, 2006 03:37 |
| Needing Help | DJ JimmyD | Web Page Design | 2 | Jul 30th, 2006 23:38 |