查找特定字母在JavaScript的句子中出现了多少次
我们需要编写一个JavaScript函数来查找特定字母在句子中出现的次数。
示例
为此的代码将是-
const string = 'This is just an example string for the program';
const countAppearances = (str, char) => {
let count = 0;
for(let i = 0; i < str.length; i++){
if(str[i] !== char){
//使用继续移动到下一个迭代
continue;
};
//如果我们到达此处,则意味着str[i]和char相同
//所以我们增加了数量
count++;
};
return count;
};
console.log(countAppearances(string, 'a'));
console.log(countAppearances(string, 'e'));
console.log(countAppearances(string, 's'));输出结果
控制台中的输出-
3 3 4
热门推荐
7 圆圆的祝福语简短
10 学生开学季祝福语简短
11 朋友花篮开业祝福语简短
12 新婚祝福语大全简短精炼
13 考试祝福语日文简短版
14 成长仪式的简短祝福语
15 3岁孩子祝福语简短
16 爱情祝福语大全 简短实际
17 生女儿的祝福语简短
18 简短的出行工作祝福语