In: Computer Science
javaScript
html receives an entry of a character string in a text box, when a button is clicked, the count of vowels in the string stored in the textbox is displayed. The html file contains one function: vowelcount().
vowelcount(): returns the number of uppercase and lowercase English language vowel letter that occurs in the string entry in the textbox.
//html:
<!--
YOUR ID
YOUR NAME
-->
<html>
<head>
<script>
function vowelcount()
{
/* YOUR CODE HERE */
}
</script>
</head>
<body>
<input type="text" name="numbers" id="numbers">
<input type="button" name="button" value="click"
onclick="vowelcount();">
<p id="result"> </p>
</body>
</html>
Answer:
Note: Updated code highlighted in bold.
Code:
<!--
YOUR ID
YOUR NAME
-->
<html>
<head>
<script>
function vowelcount()
{
//getting string from textbox
var string =
document.getElementById("numbers").value;
var listOfVowels = 'aAeEiIoOuU';
var vowelsCount = 0;
for(var i = 0; i < string.length ; i++)
{
//if character is matched with
listOfVowels
if (listOfVowels.indexOf(string[i]) !== -1)
{
vowelsCount += 1; //increment the count
}
}
//Setting result to result element
document.getElementById('result').innerHTML = "Number
of Vowels: "+vowelsCount;
}
</script>
</head>
<body>
<input type="text" name="numbers" id="numbers">
<input type="button" name="button" value="click"
onclick="vowelcount();">
<p id="result"></p>
</body>
</html>
Screenshot: