循环功能不适用于元音计数器。我如何使柜台文字加粗

马修

我的元音计数器循环功能无法正常工作,我不知道哪里出了问题,我希望对此有所帮助。

我试图使最常用的元音变黑,所以说我有,A = 34 and E = 45所以E应该像这样变黑

但是我不知道我是否丢失了一些代码,或者我的代码中是否有错误。

这是我的JavaScript。

 function countVowels() {

     var text = document.getElementById("text").value;

     var arrayOfLetters = text.split("");

     // These are the counters for the program to find the vowels.
     var countA = text.match(/[Aa]/g).length;
     var countE = text.match(/[Ee]/g).length;
     var countI = text.match(/[Ii]/g).length;
     var countO = text.match(/[Oo]/g).length;
     var countU = text.match(/[Uu]/g).length;
     var countComma = text.match(/[,.!": ;?)(]/g).length;

     var bold = "<strong>";
     var vowels = new Array();
     vowels[0] = "countA";
     vowels[1] = "countE";
     vowels[2] = "countI";
     vowels[3] = "countO";
     vowels[4] = "countU";

     for (var i = 0; i < vowels.length; i++) {
         document.getElementById("result").innerHTML = i + vowels[i] + "<br />";

     }
     // This code will output the results.
     document.getElementById("result").innerHTML = "";
     document.getElementById("result").innerHTML += "Total Letters: " + arrayOfLetters.length + "<br />";
     document.getElementById("result").innerHTML += "A's: " + countA + "<br />";
     document.getElementById("result").innerHTML += "E's: " + countE + "<br />";
     document.getElementById("result").innerHTML += "I's: " + countI + "<br />";
     document.getElementById("result").innerHTML += "O's: " + countO + "<br />";
     document.getElementById("result").innerHTML += "U's: " + countU + "<br />";
     document.getElementById("result").innerHTML += "Punctuation: " + countComma + "<br />";
 }

这是我的HTML。

<div style="text-align: center;">
    <h1> Vowel Counter </h1>
    Please enter text for your vowel count:
    <br>
    <textarea id="text" rows="10" style="width: 100%;"></textarea>
    <br>
    <button onclick="countVowels();">Count Vowels</button>
    <p id="result"></p>

这是我的摆弄

阿方索

您似乎算对了。因此,我猜剩下的唯一问题是:

  1. 获取最大数量
  2. 以粗体显示

我提到这个以获得最大的数,然后一些调整你的代码来获得以粗体显示的最大计数。

var vowels = new Array ();
vowels [0] = {"vowel" : "A", "count" : countA};
vowels [1] = {"vowel" : "E", "count" : countE};
vowels [2] = {"vowel" : "I", "count" : countI};
vowels [3] = {"vowel" : "O", "count" : countO};
vowels [4] = {"vowel" : "U", "count" : countU};
var maxCount = Math.max.apply(Math, vowels.map(function(o) {
    return o.count;
}));

for (var i = 0; i < vowels.length; i++) {
    document.getElementById("result").innerHTML += (vowels[i].count == maxCount? "<B>" : "") + vowels[i].vowel + "'s: " + vowels[i].count + "<br />";
}

请参阅此更新的小提琴

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章