Hi, I need help with the following, i'm a complete newb so bare with me I have written two functions stored in an external library file "library.js" as follows :- function isVowel(aCharacter) /************************************************************************/ /* This Function takes one argument, a string which */ /* consists of a single character. The function */ /* returns the Boolean true 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')) }; function doubleVowels(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 doubled */ /*******************************************************/ { resultString = ''; for (var count = 0; count < aString.length; count = count + 1) { if (isVowel(aString.charAt(count))) { resultString = resultString + aString.charAt(count) + aString.charAt(count); } resultString = aString.charAt(count); }; result = resultString return result; } Basically the first function should look at each char and return true or false i say 'should' as i'm not sure if it works yet , the second function takes a string and looks at each char (using the first function) to see if it's a vowel, if it is a vowel then it adds it to the string e.g " hello " becomes " heelloo ". the html to make the whole thing work, or not work is as follows :- HTML> <HEAD> <TITLE>vowels </TITLE> <SCRIPT SRC = "Library.js"> </SCRIPT> <SCRIPT > var answer; doubleVowels('hello'); /*sends string 'hello' to function doublevowels answer = doubleVowels(result); /* assigns result of function to variable window.alert(+ answer); /* displays result in alert box </SCRIPT> </HEAD> <BODY> </BODY> </HTML At the moment NAN is returned in the alert box. This is a really simple problem, but I am tearing my hair out trying to get it to work, I know i'm 99% there, please help before I have no Hair left....
I made a few revisions, it should work. Head: function isVowel(aCharacter) { return ((aCharacter == 'a') || (aCharacter == 'A') || (aCharacter == 'e') || (aCharacter == 'E') || (aCharacter == 'i') || (aCharacter == 'I') || (aCharacter == 'o') || (aCharacter == 'O') || (aCharacter == 'u') || (aCharacter == 'U')) } function doubleVowels(aString) { var resultString = ''; for(var count = 0; count < aString.length; count ++) { if(isVowel(aString.charAt(count))) resultString += aString.charAt(count) + aString.charAt(count); else resultString += aString.charAt(count); } return resultString; } Code (markup): Body: var word = 'hello'; var answer = doubleVowels(word); /* assigns result of function to variable */ window.alert(answer); /* displays result in alert box */ Code (markup):
Here's a simpler version: function isVowel(ch) { return new RegExp("[aeiou]", "i").test(ch); } Code (markup): J.D.