Javascript 循环函数不适用于元音计数器。如何使计数器文本加粗
我的循环功能,我的元音计数器在工作不正常,我不知道我哪里出了问题,我想一些帮助它 我想把最常用的元音改成粗体,比如说A=34,E=45,所以E应该像这样粗体 但是我不知道我是否丢失了一些代码,或者我的代码中是否有错误 这是我的JavaScriptJavascript 循环函数不适用于元音计数器。如何使计数器文本加粗,javascript,html,for-loop,Javascript,Html,For Loop,我的循环功能,我的元音计数器在工作不正常,我不知道我哪里出了问题,我想一些帮助它 我想把最常用的元音改成粗体,比如说A=34,E=45,所以E应该像这样粗体 但是我不知道我是否丢失了一些代码,或者我的代码中是否有错误 这是我的JavaScript function countVowels() { var text = document.getElementById("text").value; var arrayOfLetters = text.split("");
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>
这是我的
添加此选项,但在文本区域中,您无法看到粗体的Ok,下面是代码审阅/回答: 首先,我修改了这些行:
var countA = text.match(/[Aa]/g).length;
致:
因为它返回uncaughttypeerror:如果元音没有出现在控件中,则无法读取null的属性“length”。这将把它设置为0
接下来,我将数组声明简化为:
var vowels = ["countA", "countE", "countI", "countO", "countU"];
然后我删除了这个seciton,它不起任何作用,因为它稍后会被覆盖:
// does nothing
// for (var i = 0; i < vowels.length; i++) {
// document.getElementById("result").innerHTML = i + vowels[i] + "<br />";
// }
然后,为了在输出中加粗值,我创建了一个函数来比较值和最大值:
function formatValue(val, max) {
return (val === max) ? '<strong>' + val + '</strong>' : val;
}
最后一节将调用此函数以如下方式输出数字:
document.getElementById("result").innerHTML += "A's: " + formatValue(countA,maxVal) + "<br />";
完整JS
我认为你应该首先找出哪个是最常用的元音,然后将其与输出中的结果行进行比较。例如:
vowelCount = { A: countA, E: countE, I: countI, O:countO, U:countU };
// Find vowel with maximum appearances
var vowelMaxCount = -1;
var vowelMostPopular = -1;
for(vowel in vowelCount) {
if(vowelCount[vowel] > vowelMaxCount) {
vowelMaxCount = vowelCount[vowel];
vowelMostPopular = vowel;
}
}
// This code will output the results.
document.getElementById("result").innerHTML = "";
document.getElementById("result").innerHTML += "Total Letters: " + arrayOfLetters.length + "<br />";
for(vowel in vowelCount) {
v = (vowel == vowelMostPopular)?"<strong>"+vowel+"</strong>":vowel;
document.getElementById("result").innerHTML += v+"'s: " + vowelCount[vowel] + "<br />";
}
document.getElementById("result").innerHTML += "Punctuation: " + countComma + "<br />";
演示小提琴:你似乎算对了。所以我想剩下的问题只有: 获取最大计数 以粗体显示 我提到了获取最大计数,然后对代码进行一些调整,以获取以粗体显示的最大计数
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 />";
}
请参阅。看起来不错。。。至少计数似乎是正确的。到底是什么问题?你希望代码的哪一部分将文本加粗?我想说的是,如果有人在文本框中输入文本,当按下计数元音按钮时,最常用的元音将显示为加粗,而不是计数器中的文本。你应该为其编写代码,这些事情不会自动发生。。。您还应该将其输出到文本区域以外的其他位置。尝试将元音加粗的代码部分在哪里?我不知道我是否丢失了一些代码,或者我的代码中是否有错误-什么?这是你的密码还是什么?你甚至不知道你是否尝试过加粗?我不需要加粗文本区域,但结果加粗是否有效?它不起作用,我尝试过,什么都没有出现@Carbos将其插入函数中,并指定元音必须在:元音mustUsedDupCase和元音mustUsedLowCase中使用,并更改:document.getElementByIdtext.value=result;如果你想把它放在另一个房间里,我应该把它改成什么
document.getElementById("result").innerHTML += "A's: " + formatValue(countA,maxVal) + "<br />";
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) ? text.match(/[Aa]/g).length : 0);
var countE = (text.match(/[Ee]/g) ? text.match(/[Ee]/g).length : 0);
var countI = (text.match(/[Ii]/g) ? text.match(/[Ii]/g).length : 0);
var countO = (text.match(/[Oo]/g) ? text.match(/[Oo]/g).length : 0);
var countU = (text.match(/[Uu]/g) ? text.match(/[Uu]/g).length : 0);
var countComma = (text.match(/[,.!": ;?)(]/gi) ? text.match(/[,.!": ;?)(]/gi).length : 0);
var bold = "<strong>";
var vowels = ["countA", "countE", "countI", "countO", "countU"];
var maxVal = Math.max(countA,countE,countI,countO,countU);
// 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: " + formatValue(countA,maxVal) + "<br />";
document.getElementById("result").innerHTML += "E's: " + formatValue(countE,maxVal) + "<br />";
document.getElementById("result").innerHTML += "I's: " + formatValue(countI,maxVal) + "<br />";
document.getElementById("result").innerHTML += "O's: " + formatValue(countO,maxVal) + "<br />";
document.getElementById("result").innerHTML += "U's: " + formatValue(countU,maxVal) + "<br />";
document.getElementById("result").innerHTML += "Punctuation: " + countComma + "<br />";
}
function formatValue(val, max) {
return (val === max) ? '<strong>' + val + '</strong>' : val;
}
vowelCount = { A: countA, E: countE, I: countI, O:countO, U:countU };
// Find vowel with maximum appearances
var vowelMaxCount = -1;
var vowelMostPopular = -1;
for(vowel in vowelCount) {
if(vowelCount[vowel] > vowelMaxCount) {
vowelMaxCount = vowelCount[vowel];
vowelMostPopular = vowel;
}
}
// This code will output the results.
document.getElementById("result").innerHTML = "";
document.getElementById("result").innerHTML += "Total Letters: " + arrayOfLetters.length + "<br />";
for(vowel in vowelCount) {
v = (vowel == vowelMostPopular)?"<strong>"+vowel+"</strong>":vowel;
document.getElementById("result").innerHTML += v+"'s: " + vowelCount[vowel] + "<br />";
}
document.getElementById("result").innerHTML += "Punctuation: " + countComma + "<br />";
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 />";
}