编写JavaScript函数以获取指定字符串中每个字母的出现次数

时间:2018-02-28 17:59:42

标签: javascript

“编写JavaScript函数以获取指定字符串中每个字母的出现次数。” 我试过这种方式,但我的所有输出都是0,我真的不明白为什么。

我的想法是: 字母顺序 - 所以如果一个字母与下一个字母相同,则计数器会增加。当它不相同时,它会记录字母,它出现的次数并重置计数器。

顺便说一句,我不知道如何让它读出只出现过一次的字母。你能帮忙吗?

function count(string) {
  let string1 = string.split("").sort().join("");
  let counter = 0;
  for (let i = 0; i < string.length; i++) {
    if (string1[i] == string[i + 1]) {
      counter++;
    } else {
      console.log(string1[i] + " " + counter);
      counter = 0;
    }
  }
}
count("thequickbrownfoxjumpsoverthelazydog");

5 个答案:

答案 0 :(得分:2)

使用函数Google Play App Signing可以避免只出现一次问题。

function count(string) {
  return string.split("").reduce((a, letter) => {
    a[letter] = (a[letter] || 0) + 1;
    return a;
  }, {});
}
console.log(count("thequickbrownfoxjumpsoverthelazydog"));
.as-console-wrapper { max-height: 100% !important; top: 0; }

带解释的片段

function count(string) {
  return string.split("").reduce((a, letter) => {
    var currentCount = a[letter];
    if (currentCount) { 
      currentCount = currentCount + 1; // If previously counted + 1
    } else {
      currentCount = 1; // Else initialize with first occurence.
    }
    
    a[letter] = currentCount; //Store the new count.
    
    return a;
  }, {});
}

console.log(count("thequickbrownfoxjumpsoverthelazydog"));

资源

答案 1 :(得分:1)

代码中有两个小错误。

  • 匹配条件应为string1[i] == string1[i + 1]
  • 启动值为1的计数器,因为每个值至少会出现一次。

&#13;
&#13;
function count(string) {
  let string1 = string.split("").sort().join("");
  let counter = 1;
  for (let i = 0; i < string.length; i++) {
    if (string1[i] == string1[i + 1]) {
      counter++;
    } else {
      console.log(string1[i] + " " + counter);
      counter = 1;
    }
  }
}
count("thequickbrownfoxjumpsoverthelazydog");
&#13;
&#13;
&#13;

我建议你使用一种不同的方法,使用.reduce并返回一个很好的计数对象。

&#13;
&#13;
function count(string) {
  return string.split("").reduce(
    (acc, el) => {
      if(acc.hasOwnProperty(el))
        acc[el]++;
      else
        acc[el] = 1;
      return acc;
    }, {}
  )
}
var data = count("thequickbrownfoxjumpsoverthelazydog");
console.log(data);
&#13;
&#13;
&#13;

答案 2 :(得分:1)

我在这里没有看到的另一种方法:

const count = (str) => {
  let freq = {};
  for(let i = 0; i < str.length; i++) { // you can use for...of instead!
    const currentLetter = str.charAt(i);
    freq[currentLetter] = freq[currentLetter] + 1 || 1;
  }
  return freq;
}
console.log(count("thequickbrownfoxjumpsoverthelazydog"));

  1. 创建空对象
  2. 如果密钥不存在,请将字母指定为键并将值<1> > 的值设置为1。
  3. 返回对象。

答案 3 :(得分:0)

你可以这样做

function count(text){
  var i = 0;
  var j = 0;
  var chars = new Array();
  var occurs = new Array();

  for(i = 0;i < text.length;i++){
    //save current char
    chars.push(text[i]);

    //get occurences
    occurs.push(countOccurs(text, text[i]));
  }

  //clean for duplicates
  for(i = 0;i < chars.length;i++){
    for(j = (i + 1);j < chars.length;j++){
      if(chars[i] == chars[j]){
        chars[j] = "";
        occurs[j] = 0;
      }
    }
  }

  //print it!
  for(i = 0;i < chars.length;i++){
    if(chars[i] != '')
      console.log("The char " + chars[i] + " appears " + occurs[i] + " times.");
  }
}

function countOccurs(text, character){
  var i = 0;
  var ret = 0;

  for(i = 0;i < text.length;i++)
    if(text[i] == character)
      ret++

  return ret;
}

count("abcdefabcd");

所以你只计算每个char的发生然后清理数组。

答案 4 :(得分:0)

case