简体   繁体   English

使用Javascript具有两个参数的非递归函数

[英]non-recursive function with two parameters using Javascript

I am trying to create a function with two parameters that will count how times a character repeats in a given string.我正在尝试创建一个带有两个参数的函数,该函数将计算一个字符在给定字符串中的重复次数。 I have my code below but it is not working properly.我有下面的代码,但它无法正常工作。 Also, I am trying to make it a non-recursive function.另外,我试图使它成为一个非递归函数。 Can anyone help me with that?任何人都可以帮助我吗?

function count(char,word) {
  var a,b;
  a= char
  b= word
  c= b.indexof(a)
  return c
}

count('a','apple')

To count the occurrences of a string inside another one, you can split the latter using the former as the separator, and then use length to count the items of the returned array.要计算一个字符串在另一个字符串中的出现次数,可以使用前者作为分隔符split后者,然后使用length计算返回数组的项。

function count(char, word) {
  return word.split(char).length - 1;
}

If what you mean by your question is "how many occurrences of a given character are there in a word", then there are several possibilities.如果您的问题的意思是“一个单词中给定字符出现了多少次”,那么有几种可能性。 You can loop calling .indexOf() , you can let a regex find all the matches or you can just iterate through the string manually comparing.您可以循环调用.indexOf() ,您可以让正则表达式找到所有匹配项,或者您可以手动遍历字符串进行比较。

If you meant something different by your question, please clarify the question.如果您的问题有不同的意思,请澄清问题。

Here are some possibilities:这里有一些可能性:

 function count(char, word) { var cntr = 0; var pos = 0; while ((pos = word.indexOf(char, pos)) !== -1) { ++cntr; ++pos; } return cntr; } var cnt = count('a','apple'); document.write(cnt + "<br>"); cnt = count('p','apple'); document.write(cnt);

Or, you could use a regex if your character is not a special regex character:或者,如果您的角色不是特殊的正则表达式字符,则可以使用正则表达式:

 function count(char, word) { var cnt = word.match(new RegExp(char, "g")); return cnt ? cnt.length: 0; } var cnt = count('p', 'apple'); document.write(cnt);

Or, you could just iterate through the string and manually count:或者,您可以遍历字符串并手动计数:

 function count(char, word) { var cnt = 0; for (var i = 0; i < word.length; i++) { if (word.charAt(i) === char) { ++cnt; } } return cnt; } var cnt = count('p', 'apple'); document.write(cnt);

This is a very simple loop that iterates through the word , counting the instances of ch .这是一个非常简单的循环,它遍历单词,计算ch的实例。

function count(ch,word) {
  var i,count = 0;
  for(i = 0; i < word.length; i++) {
    if (word[i] === ch) count++;
  }
  return count;
}

console.log(count('p','apple'));

You need loops:你需要循环:

 function count(c, word) { var i = 0; var arr = word.split(''); //break into array var a; while (arr.length) { //until array runs out of letters a = arr.pop(); // pull out one by one if (a === c) // got match i++; } return i; } alert(count('a', 'apple'));

The simplest way would probably be just looping through the input.最简单的方法可能只是循环输入。 Like this:像这样:

function count(char,word) {
  var count = 0;
  for(var i = 0; i < word.length; i++)
  {
    if(word.charAt(i) === char)
      count++;
  }
  return count;
}

Here's a way you can do this with a regular expression:这是您可以使用正则表达式执行此操作的一种方法:

function countLetter(letter,word) {
  var regexp = New RegExp(letter, 'g') //searches word for letter
  var arr = word.match(regexp);        //finds the letter within the word,
  return arr == null ? 0 : arr.length;
}                                      //sticks that letter into arr for each time it
                                       //appears in the word

console.log(countLetter('a','apple a a a '));
//arr = ['a', 'a', 'a', 'a'], arr.length = 4
console.log(countLetter('p','apples'));
//arr = ['p','p'] arr.length = 2

Hope this helps!希望这可以帮助!

I would go with an accumulator function for simply counting occurrences我会使用累加器功能来简单地计算出现次数

function count(char,word) {
    return [].reduce.call(word,function(p,c){
        return p += char == c ? 1 : 0;
    },0);
}

However, that would just be for totals.但是,这仅适用于总数。 In order to count "in a row" is much different and would require tracking the repetitions为了计数“连续”有很大不同,需要跟踪重复

function count(char,word) {
    var total = 0;
    var max = 0;
    [].reduce.call(word.substr(1,word.length),function(p,c){
        if(p == c && c == char){ total++ }else{
            if( total > max ) max = total;
            total = 0;
        }
        return c;
    },word[0]);
    return max > 0 ? max + 1 : 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM