使用 JavaScript 统计并返回 str1 中出现在 str2 中的字符数
问题
我们需要编写一个JavaScript函数,它接受两个字符串str1和str2分别作为第一个和第二个参数。
我们的函数应该统计并返回str1中出现在str2中的字符数,如果出现重复出现,我们需要单独统计。
例如,如果函数的输入是
输入
const str1 = 'Kk'; const str2 = 'klKKkKsl';
输出
const output = 5;
示例
以下是代码-
const str1 = 'Kk';
const str2 = 'klKKkKsl';
var countAppearances = (str1 = '', str2 = '') => {
const map = {}
for(let c of str1) {
map[c] = true
}
let count = 0
for(let c of str2) {
if(map[c]) {
count+=1
}
}
return count
};
console.log(countAppearances(str1, str2));输出结果5
热门推荐
10 新人红包祝福语简短精辟
11 幼师给毕业祝福语简短
12 生日高考祝福语妹妹简短
13 下飞机祝福语简短英文
14 元宵发客户祝福语简短
15 爸妈金婚文案祝福语简短
16 新郎朋友红包祝福语简短
17 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短