js写出你的名字的拼音,判断哪个字母出现的最多

function fn(str) {
    var obj = {};
    for (var i = 0; i < str.length; i++) {
        if (!obj[str.charAt(i)]) {
            obj[str.charAt(i)] = 1;
        } else {
            obj[str.charAt(i)]++;
        }
    }
    var value = '';
    var count = 0;
    for (var i in obj) {
        if (obj[i] > count) {
            count = obj[i];
            value = i;
        }
    }
    console.log('出现次数最多的是' + value + ',出现次数为' + count);
}

猜你喜欢

转载自www.cnblogs.com/gxx129/p/10981406.html