【问题标题】:I am looking to create an Array from the Output of Counts conducted by my string我正在寻找从我的字符串进行的计数输出创建一个数组
【发布时间】:2016-06-23 00:53:17
【问题描述】:

我正在尝试制作下面的 JS 代码,Develop 和 Array 每个元音的计数 1) A 的计数 2) E 的计数 3) 我的计数 4) 计数为 0 5) U 计数

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

我知道这与创建变量有关

var largeSoFar 等...但是如何将它拼凑起来我遇到了问题。

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

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();
    }

【问题讨论】:

  • 为什么不使用对象来计数,以单个字母为键?
  • 你好,是的,我接下来会这样做,但我更容易理解这个版本的逻辑,但我知道有一些方法可以简化实际结构本身。
  • 你会怎么做

标签: javascript html


【解决方案1】:

为什么不这样呢?你提供string,它会返回一个object,每个元音都有一个计数?

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;
}

当您返回结果 object 后,您可以进行检查,看看哪个是最常发生的。

【讨论】:

  • 谢谢你,所以你说我用单独的 Var 的方式并不正确
  • 真的没必要,当您可以返回一个对象时,您正在创建 15+ 个变量。取回对象后,您可以进行简单的比较以查看哪个数字最大等。
  • "document.getElementById('TotalU').value = result.u;"这就是我在 HTML 文件中的称呼吗
  • 在格式中我有 15 个以上的变量...我将如何创建一个变量来找到其中的最大值...是吗 // var Array=[count4,count5,count6 ,count7,count8];//
  • var max = Object.keys(result).reduce(function(a, b){ return result[a] &gt; result[b] ? a : b }); 你可能需要这样的东西。
【解决方案2】:

另一种方法可能是

var str = "When found, separator is removed from the string and the substrings are returned in an array. If separator is not found or is omitted, the array contains one element consisting of the entire string. If separator is an empty string, str is converted to an array of characters. If separator is a regular expression that contains capturing parentheses, then each time separator is matched, the results (including any undefined results) of the capturing parentheses are spliced into the output array. However, not all browsers support this capability.",
 vowels = {"a":0,"e":0,"i":0,"o":0,"u":0},
     ok = Object.keys(vowels);
 maxvow = "";
for (var i = 0, len = str.length; i < len; i++ ){
  var chr = str[i].toLowerCase();
  chr in vowels && ++vowels[chr]; // or ok.includes(chr) && ++vowels[chr];
}
maxvow = ok.reduce((p,k) => vowels[k] > p[0] ? [vowels[k],k] : p,[0,""])[1];
console.log(vowels);
console.log(maxvow);

【讨论】:

    【解决方案3】:

    您可以拆分字符串并使用一个对象来计算元音和其他字母。

    var count = { a: 0, e: 0, i: 0, o: 0, u: 0, other: 0 },
        test = 'Es war einmal ein schwarzes Kaninchen';
    
    test.toLowerCase().split(/(?=[a-z])/).forEach(function (c, i) {
        if (c[0] in count) {
            count[c[0]]++;
        } else {
            count.other++;
        }
    });
    
    console.log(count);

    【讨论】:

      猜你喜欢
      • 2017-12-19
      • 2018-01-08
      • 2015-07-24
      • 1970-01-01
      • 2019-02-16
      • 1970-01-01
      • 2019-02-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多