我想检查元音字母的输入值,然后计算其中的元音数量。
我只知道如何检查元音的输入,我发现很难知道元音的数量,我想能够写:“这篇文章包含6个元音”。
请看一下代码和帮助。
//Selecting document element
const voutText = document.querySelector('#vText');
const voutBtn = document.querySelector('#vSubBtn');
const voutBox = document.querySelector('#vOutput');
const vowelCount = 0;
const voutApp = () => {
const vowel = ['A','E', 'I', 'O', 'U'];
const voutTextView = voutText.value.toUpperCase();
for (i = 0; i < voutText.value.length; i++) {
voutBox.innerHTML = i;
}
if (voutTextView.includes('A')) {
alert("Yey! we found vowels, your text contains the vowel sound 'A'");
} else if (voutTextView.includes('E')) {
alert("Yey! we found vowels, Your text contains the vowel sound 'E'");
} else if (voutTextView.includes('I')) {
alert("Yey! we found vowels, Your text contains the vowel sound 'I'");
} else if (voutTextView.includes('O')) {
alert("Yey! we found vowels, Your text contains the vowel sound 'O'");
} else if (voutTextView.includes('U')) {
alert("Yey! we found vowels, Your text contains the vowel sound 'U'");
} else {
voutBox.innerHTML = 'Your text contains no vowel sound';
}
voutText.value = '';
voutBox.innerHTML = `Your text ${voutTextView} contains vowel sounds`;//I have to figure out how to get the vowel counts EG: Your text (Input Value) contains 6 vowels.
}
voutBtn.addEventListener('click', voutApp);
<div id="vHead">
<h1>VOUT</h1>
<p>Check a word for vowel sounds</p>
<input id="vText" type="text" placeholder="Eg: Victor" value="Victor" required>
<input id="vSubBtn" type="button" value="Check">
</div>
<div id="vOutput"></div>
4条答案
按热度按时间waxmsbnn1#
正则表达式可以匹配元音,然后可以检查匹配数。
l5tcr1uw2#
字符串在js中是可编辑的,因此您可以使用spread操作符获取字符数组并检查它是否为
vowel
或者如果它在vowel
使用includes方法的数组ars1skjm3#
其他一些帖子展示了如何缩短代码以实现这一目标。如果你想保持你所写的风格,比如使用
for
循环和alert
s等,然后您可以这样做:fgw7neuy4#
检查这个