Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Javascript的具有两个参数的非递归函数_Javascript - Fatal编程技术网

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

使用Javascript的具有两个参数的非递归函数,javascript,Javascript,我试图创建一个带有两个参数的函数,用于计算字符在给定字符串中的重复次数。我有下面的代码,但它不能正常工作。另外,我正试图使它成为一个非递归函数。有人能帮我吗 function count(char,word) { var a,b; a= char b= word c= b.indexof(a) return c } count('a','apple') 如果你的问题的意思是“一个单词中有多少个给定字符出现”,那么有几种可能性。您可以循环调用.indexOf(),您可以让正

我试图创建一个带有两个参数的函数,用于计算字符在给定字符串中的重复次数。我有下面的代码,但它不能正常工作。另外,我正试图使它成为一个非递归函数。有人能帮我吗

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

count('a','apple')

如果你的问题的意思是“一个单词中有多少个给定字符出现”,那么有几种可能性。您可以循环调用
.indexOf()
,您可以让正则表达式查找所有匹配项,或者您可以手动遍历字符串

如果你的问题有不同的意思,请澄清问题

以下是一些可能性:

函数计数(字符、字){
var-cntr=0;
var-pos=0;
while((pos=word.indexOf(char,pos))!=-1){
++碳纳米管;
++pos;
}
返回cntr;
}
var cnt=计数('a','apple');
文件。写入(cnt+“
”); cnt=计数('p','apple');
文件编写(cnt)这是一个非常简单的循环,它遍历单词,计算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'));
函数计数(ch,word){
变量i,计数=0;
for(i=0;i
您需要循环:

函数计数(c,字){
var i=0;
var arr=word.split(“”);//分解为数组
var a;
而(arr.length){//,直到数组中的字母用完为止
a=arr.pop();//逐个拉出
如果(a==c)//得到匹配
i++;
}
返回i;
}

警报(计数('a','apple')最简单的方法可能就是循环输入。像这样:

function count(char,word) {
  var count = 0;
  for(var i = 0; i < word.length; i++)
  {
    if(word.charAt(i) === char)
      count++;
  }
  return count;
}
函数计数(字符、字){
var计数=0;
for(变量i=0;i
要计算字符串在另一个字符串中的出现次数,可以使用前者作为分隔符,然后使用来计算返回数组的项

函数计数(字符、字){
返回单词.split(char).length-1;
}

下面是一种使用正则表达式执行此操作的方法:

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

希望这有帮助

我会使用累加器函数来简单地计数事件

function count(char,word) {
    return [].reduce.call(word,function(p,c){
        return p += char == c ? 1 : 0;
    },0);
}
然而,这只是总数。为了计数,“一行”是非常不同的,需要跟踪重复

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

检查这里。计算一个字符重复的次数。不知道为什么答案会漏掉这个。@Oriol-我相信问题是这个字符在单词中重复了多少次。例如,据说l不应该是3。重复表示连续,出现表示全部。”l'出现三次,但在一点上重复两次。@TravisJ我认为“重复”不一定是“连续重复”。请注意
arr
如果不匹配,则将
null
。因此,
arr.length
将抛出。请添加一个说明,说明如何解决询问者问题。