【问题标题】:Javascript: Function to fill an object with the vowels as the keys and the count as the valuesJavascript:用元音作为键和计数作为值填充对象的函数
【发布时间】:2015-06-28 02:55:50
【问题描述】:

和以前一样,我到处寻找答案,我只是一个初学者,我正在努力从中学习,而不仅仅是得到答案。

var voweler = function (str) {
  var strArr = str.split('')
  var obj = {};
  for (var i = 0; i < strArr.length; i++) {
    if (strArr[i] == 'a') {
      obj.a = 0;
      obj.a++;
    } else if (strArr[i] == 'e') {
      obj.e = 0;
      obj.e++;
    } else if (strArr[i] == 'i') {
      obj.i = 0;
      obj.i++;
    } else if (strArr[i] == 'o') {
      obj.o = 0;
      obj.o++;
    } else if (strArr[i] == 'u') {
      obj.u = 0;
      obj.u++;
    }
  };
  return obj;
}
voweler("This is a test")
//returns this which is wrong. Object {i: 1, a: 1, e: 1}

【问题讨论】:

  • 还有什么问题?
  • 为什么不计算第二个字母 i?
  • 密切注意你使用的逻辑 - 用简单的英语大声朗读真的很有帮助:“如果这个字符是字母 i,将 obj.i 设置为零,然后递增”。
  • @adeneo 在第 8 行,我从未见过这样的东西,我不明白它在做什么。

标签: javascript function object for-loop


【解决方案1】:

您更新计数的代码有误。遇到的每个元音,你运行obj.&lt;vowel&gt; = 0 重置计数!要解决这个问题,请在进入 for 循环之前设置计数,然后在 for 循环中,只增加计数器。

如果您希望仅在元音存在时有一个条目,您可以有条件地递增:

if(strArr[i] == <some_vowel>){
    if(obj.<some_vowel> === undefined)obj.<some_vowel> = 1;
    else obj.<some_vowel> ++;
}

【讨论】:

  • 我想避免 o: 0 在那里。
  • 为什么会这样?
  • 理想情况下,我宁愿不返回空白,就像我在其他地方使用这种类型的代码时,我不想返回大量的零。
  • @SpencerShattuck 查看编辑
【解决方案2】:

你可能更喜欢这样的东西:

function voweler(input) {
    var result = {
        a: 0, e: 0, i: 0, o: 0, u: 0
    };

    for (var i = 0; i < input.length; i++) {
        var char = input.charAt(i).toLowerCase();

        if (result.hasOwnProperty(char)) {
            result[char] ++;
        }
    }

    return result;
}

【讨论】:

    【解决方案3】:

    几个提示:

    • 每次字符为元音时,循环都会将键的属性值分配为 0,然后再递增。
    • 如果要查找大写和小写元音,可以使用 toLowerCase()。
    • 您可以使用indexOf。如果在字符串中找不到参数,它将返回 -1。

    var voweler = function (str) {
      var strArr = str.toLowerCase().split('');
      var obj = {};
      strArr.forEach(function(ch) {
        if ('aeiou'.indexOf(ch) !== -1) {
          obj[ch] = (obj[ch] || 0 ) + 1;
        }
      });
      return obj;
    }
    console.log(voweler("This is a test"));
    // Object {i: 2, a: 1, e: 1}

    【讨论】:

      【解决方案4】:

      仅仅因为我们可以……而 DTing 已经有了你的答案。

      function countVowels(s) {
        var vowels = /[aeiou]/,
                 o = {};
      
        s.toLowerCase().split('').forEach(function(c){
          if (vowels.test(c)) o.hasOwnProperty(c)? ++o[c] : o[c] = 1;
        });
        return o;
      }
      
      console.log(JSON.stringify(countVowels('hi there')));
      

      还有:

      function countVowels(s) {
        return (s.toLowerCase().match(/[aeiou]/g) || []).reduce(function(o, c) {
           o[c] = (o[c] || 0) + 1;
           return o;
        }, {});
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-15
        • 2019-09-25
        • 2011-04-04
        • 1970-01-01
        相关资源
        最近更新 更多