0

Hey everyone,
I literally just started to learn Javascript today and started with a function that takes in a string and tells you the number of vowels (a,e,I,o,u) in it. It works, but when I use document.write(vowel("hello")) in my body, instead of just giving an output of "this string has 2 vowels in it!", it outputs:

this string has 2 vowels in it!
undefined

Whats going on, here is my code for the function:

function vowel(string)
		{
			var count = 0;
			string = string.toLowerCase();
			for (i=0;i<string.length;i++) 
			{
				if (string.charAt(i) == "a" || string.charAt(i) == "e" ||  string.charAt(i) == "i" || string.charAt(i) == "o" ||  string.charAt(i) == "u")
				{
					count++;
				}
			}
				document.write("this string has " + count + " vowels in it! <br />");
		}

Edited by sgriffin: I figured it out: needed a return statement

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by kracko
0

Hey. I didn't realy get what's your mistake..just corrected it my way and it works fine. Look..

<script>
function vowel(str){
var count = 0;
var myStr=new String(str);
myStr = myStr.toLowerCase();
for (i=0;i<myStr.length;i++){
if (myStr.charAt(i) == "a" || myStr.charAt(i) == "e" ||  myStr.charAt(i) == "i" || myStr.charAt(i) == "o" ||  myStr.charAt(i) == "u"){
count++;}
}
document.write("this string has " + count + " vowels in it! <br />");
}
vowel("hello");
</script>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.