gpt4 book ai didi

javascript - 我希望根据字符串进行的计数输出创建一个数组

转载 作者:行者123 更新时间:2023-11-28 05:52:36 24 4
gpt4 key购买 nike

我正在尝试制作下面的 JS 代码、开发和数组每个元音的个数1)A的计数2)E的计数3) 数我4) 计数 05)U计数

读取字符串代码后,生成每个元音出现的次数。但现在我需要创建一个数组来显示出现最频繁的元音

我知道这与创建变量有关

var MaximumSoFar 等...但是如何将它们拼凑在一起我遇到了问题。

/* * 要更改此模板,请选择“工具”|“模板 * 并在编辑器中打开模板。 */

function count_all() {
var str = document.getElementById('txtname').value;
var count9=0, totalvowels="";
var count2=0, totalconsonants="";
var count3=0, total_digits="";
var count4=0, totalA="";
var count5=0, totalE="";
var count6=0, totalI="";
var count7=0, totalO="";
var count8=0, totalU="";

for (var i = 0; i < str.length; i++) {
if (str.charAt(i).match(/[a-zA-Z]/) !== null) {
if (str.charAt(i).match(/[aeiouAEIOU]/))
{
totalvowels = totalvowels + str.charAt(i);
count9++;
}
if (str.charAt(i).match(/[bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ]/))
{
totalconsonants = totalconsonants + str.charAt(i);
count2++;
}
if (str.charAt(i).match(/[aA]/))
{
totalA = totalA + str.charAt(i);
count4++;
}

if (str.charAt(i).match(/[eE]/))
{
totalE = totalE + str.charAt(i);
count5++;
}

if (str.charAt(i).match(/[iI]/))
{
totalI = totalI + str.charAt(i);
count6++;
}

if (str.charAt(i).match(/[oO]/))
{
totalO = totalO + str.charAt(i);
count7++;
}

if (str.charAt(i).match(/[uU]/))
{
totalU = totalU + str.charAt(i);
count8++;
}

}

function retnum(str1) {
var num = str1.replace(/[^0-9]/g, '');
return num;
}
function count_digits(str2) {
var num2 = str2.replace(/[^0-9]/g,"").length;
return num2;
}

}
document.getElementById('TotalU').value = count8;
document.getElementById('TotalO').value = count7;
document.getElementById('TotalI').value = count6;
document.getElementById('TotalE').value = count5;
document.getElementById('TotalA').value = count4;
document.getElementById('consonant_counter').value = count2;
document.getElementById('total_consonants').value = totalconsonants;
document.getElementById('vowels').value = totalvowels;
document.getElementById('vocount').value = count9;
document.getElementById('digits1').value = count_digits(str);
document.getElementById('digits2').value = retnum(str);
}

function clear_all()
{
document.getElementById('TotalU').value = "";
document.getElementById('TotalO').value = "";
document.getElementById('TotalI').value = "";
document.getElementById('TotalE').value = "";
document.getElementById('TotalA').value = "";
document.getElementById('consonant_counts').value ="";
document.getElementById('total_consonants').value ="";
document.getElementById('vowels').value = "";
document.getElementById('vcount').value = "";
document.getElementById('digits1').value ="";
document.getElementById('digits2').value ="";
document.getElementById('txtname').value ="";
document.getElementById('txtname').focus();
}

最佳答案

为什么不做这样的事情呢?您提供字符串,它会返回一个对象,其中包含每个元音的计数?

function countVowels(str) {

var result = {};
result.a = 0;
result.e = 0;
result.i = 0;
result.o = 0;
result.u = 0;

for (var i = 0, len = str.length; i < len; i++) {
switch(str[i].toLowerCase()) {
case "a":
result.a = result.a + 1;
break;
case "e":
result.e = result.e + 1;
break;
case "i":
result.i = result.i + 1;
break;
case "o":
result.o = result.o + 1;
break;
case "u":
result.u = result.u + 1;
break;
default:
break;
}
}

return result;
}

当您返回结果对象时,您可以进行检查以查看哪个是最常出现的。

关于javascript - 我希望根据字符串进行的计数输出创建一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37971457/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com