js输出单一字符字串

<!DOCTYPE HTML>
<html>
<body>
    <input type="text" id="str" >
    <input type="button" value="click" onclick="test()"> 

</body>
</html>
<script>
function test(){
    //alert("123");
    var str = document.getElementById("str").value;
    if (str.length <= 1) {
        alert(str);
    } else {
        var maxLen = 0;
        var len = 1;
        var maxCur = str[0];
        var cur = str[0];
        for (var i = 0; i < str.length - 1; i++) {
            if (str[i] == str[i+1]) {
                cur = str[i];
                len ++;
            } else {
                len = 1;
            }
            if (len > maxLen) {
                maxCur = cur;
                maxLen = len;
            };
        };

        var strLen = new Array(maxLen+1).join(maxCur);
        alert(strLen);
    };
}
</script>

 

posted on 2015-05-27 22:42  Shower稻草人  阅读(242)  评论(0编辑  收藏  举报