1.创建一个html。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<!-- 将js引入到html中 -->
<!-- <script src="./17.每一个字符出现的次数.js"></script> -->
</body>
</html>
2.编写js代码
let str = "abacdkcaa";
let charCount = {};
for (let i = 0; i < str.length; i++) {
let char = str[i];
if (charCount[char]) {
charCount[char]++;
} else {
charCount[char] = 1;
}
}
console.log(charCount);
3.运行结果如下:(首先F12打开控制台,即可查看结果!!!)