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 />");
		}

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>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.