Created
May 23, 2014 22:20
-
-
Save jikeytang/b52557ef3494bec0f3dd to your computer and use it in GitHub Desktop.
[ Javascript ] - 20140524-题目2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
根据给定字符串进行按位数反转,如:字符串abcdef按2位反转得到字符串cdefab | |
PS: | |
1. 回复时注意加上下面这句话,才会有语法高亮或格式缩进。 | |
```javascript | |
// you code | |
``` | |
2. 粘贴代码时请使用shift+tab,缩进前面的空白。 |
var a ="abcdef",
_a = a.substr(0,2);
a.replace(_a,'')+_a;
function flip(s, offset) {
var len = s.length;
offset = offset < len ? offset : (offset % len);
return s.replace(new RegExp('^(.{' + offset + '})(.*)$'), '$2$1');
}
str="xsdsada1232xa";
len=str.length-1;
newstr="";
while(len+1){
newstr+= str[len];
len--;
}
// you code
var str = "abcdefg";
alert(str.split("").reverse().join(""));
反转的话,直接用数组的reverse反转方法
function reverseStr(str, n){
var arr = str.split('');
var res = [];
while(arr.length){
res = arr.splice(0,n).concat(res);
}
return res.join('');
}
console.log(reverseStr('adefefef', 4));
function newStr(str , num){
var newStr = str.slice(num) + str.slice(0,num) ;
return newStr;
}
console.log( newStr('abcdef',2) )
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
理解不了按两位反转得到:cdefab ? 还是说是:efcdab