Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2014年11月13日 #1

Open
nunnly opened this issue Nov 13, 2014 · 10 comments
Open

2014年11月13日 #1

nunnly opened this issue Nov 13, 2014 · 10 comments
Labels

Comments

@nunnly
Copy link
Owner

nunnly commented Nov 13, 2014

/*
* 在String对象的原型上创建一个方法,实现以下功能
* "String".reverse() // return "gnirtS"
* "Super awesome string here" //return "ereh gnirts emosewa repuS"
*
* */

String.prototype.reverse = function(){

};

//测试代码
"asdf".reverse() === "fdsa"
@businiaowa
Copy link

String.prototype.reverse = function(){
    var arr = this.split("");
    arr.reverse();
    return arr.join("");
}

代码可以使用三个 `开始和结束
hello! Nunn

@soulcm
Copy link

soulcm commented Nov 13, 2014

String.prototype.reverse = function(){
    var res = this.split('');
    return res.reverse().join('');
}

@nunnly
Copy link
Owner Author

nunnly commented Nov 13, 2014

不需要创建新对象的哦,想想之前虫哥提到的,利用原生的Array.prototype.reverse方法

@zhangdihong
Copy link

String.prototype.reverse = function(){
var arr=this.split("");
for(var i=0;i<arr.length/2;i++)
{
var temp=arr[arr.length-i-1];
arr[arr.length-i-1]=arr[i];
arr[i]=temp;
}
return arr.join("");
}唉,不符合题,还是目的实现了

@qingo
Copy link

qingo commented Nov 13, 2014

String.prototype.reverse = function(){
     return Array.prototype.reverse.call(this.split('')).join('');
};

@teabyii
Copy link

teabyii commented Nov 13, 2014

String.prototype.reverse = function () {
  return this.split('').reverse().join('');
}
console.log('123'.reverse() === '321');

@jiehe
Copy link

jiehe commented Nov 19, 2014

String.prototype.reverse = function(){
return Array.prototype.reverse.call(this.split("")).join("")
};

@CraigZeng
Copy link

String.prototype.reverse = function(){
  return Array.prototype.slice.call(this).reverse().join('');
}

@cuining
Copy link

cuining commented Dec 4, 2014

String.prototype.reverse=function(){return this.split('').reverse().join('')}

@nunnly nunnly added 难度6 and removed question labels Jan 23, 2015
@lenqwang
Copy link

String.prototype.reverse = function() {
   var newStr = '', i = this.length;
   for(; i >= 0; i--) {
        newStr += this.charAt(i);
   }
   return newStr;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

10 participants