js关于字符串的两道题
1.编写函数
rightchange(char,n)
,实现字符串char 循环右移n位
function rightchange(char,n){ var arr = char.split(""); n = n % arr.length;//取余 for(var i=1;i<=n;i++){ arr.unshift(arr.pop()); } console.log(arr.join("")); } rightchange("abcdefg",8);
2 编写函数
maxr(char)
返回字符串char中最长的连续重复字母
function maxr(char){ var a = 0; var b=1;
var maxlength = 0; var maxchar = "";
while(a<=char.length-2){ if(char.charAt(a) == char.charAt(b)){ b++; }else{ if(b-a>maxlength){ maxlength = b-a; maxchar = char.slice(a,b); } a = b; b = a+1; } } console.log(maxchar,maxlength); } var str = "mmjiiikkkmmmmmmmmllllllllllllllllllmlll" maxr(str);
时间: 2024-09-27 10:32:06