问题:
var str1 = "abcdabcabcaabeeeeeee";
var str2 = "fhjdiovjdasklgudsaklfgdasklfjadkslfds";
定义成一个函数,函数接受一个参数,统计该参数,每个字符各出现了多少次(统计字符串中,每个字符各出现了多少次)
打印出出现次数最多的字符,出现了几次
<!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> <script> var str1 = "abcdabcabcaabeeeeeee"; var str2 = "fhjdiovjdasklgudsaklfgdasklfjadkslfds"; var sum = 0; var arr = []; function count(str1) { for (var i = 0; i < str1.length; i++) { var arr1 = str1.split(‘‘); if (arr.indexOf(arr1[i]) == -1) { arr.push(arr1[i]); } } console.log(arr1) for (var j = 0; j < arr.length; j++) { for (var m = 0; m < arr1.length; m++) { if (arr[j] == arr1[m]) { sum += 1; } } console.log(arr[j] + ‘次数为:‘ + sum); sum = 0; } } count(str2); </script> </body> </html>