-2 javascript
我正在尝试创建一个带有两个参数的函数,这两个参数将计算字符在给定字符串中重复的次数.我的代码如下,但它无法正常工作.另外,我试图使它成为一个非递归函数.任何人都可以帮助我吗?
function count(char,word) {
var a,b;
a= char
b= word
c= b.indexof(a)
return c
}
count('a','apple')
Run Code Online (Sandbox Code Playgroud)
要计算另一个字符串中出现的字符串,可以split使用前者作为分隔符,然后使用它length来计算返回数组的项目.
function count(char, word) {
return word.split(char).length - 1;
}
Run Code Online (Sandbox Code Playgroud)