View Single Post
Old 11-12-2012, 06:13 PM   PM User | #6
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 961
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
Quote:
Originally Posted by sunfighter View Post
I'm sure RegEx would make this a snap
Slightly simpler but easily avoided for a beginner. I suspect the original intention may have been to build a string from the extracted vowels, otherwise counting the consonants would be unnecessary.
Code:
<!DOCTYPE html >
<html>
<head>
<title>TEST</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">

function calculateName( str )
{
  var vowelString = "", ch;
  
  for( var i = 0; (ch = str.charAt( i )); i++ )
    if( "aeiouAEIOU".indexOf( ch ) > -1 )
      vowelString += ch;
  
  document.getElementById('here').innerHTML = vowelString;
  
  return str.length - vowelString.length;
}

</script>
</head>
<body>
<button onclick="alert('Consonants: '+calculateName('BillyJoeBoo'))" >PUSH</button>
<div id="here"></div>
</body>
</html>
Logic Ali is offline   Reply With Quote