const str = "ddasdasdasdasdasfsssssssssssssssssss";
function checkStr(str) {
// 使用对象来储存出现的次数
let obj = {};
for (let i = 0; i < str.length; i++) {
obj[str.charAt(i)]
? (obj[str.charAt(i)] += 1)
: (obj[str.charAt(i)] = 1);
}
// 定义最大次数
let maxStr,
maxNum = 0;
// 遍历对象
for (let k in obj) {
if (obj[k] > maxNum) {
maxNum = obj[k];
maxStr = k;
}
}
return `出现次数最多的字符是${maxStr},共出现了${maxNum}次`;
}
console.log(checkStr(str));
</script>