js输出单一字符字串

时间:2024-09-30 15:33:38
<!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>