Javascript String reverseStr()
//Write a JavaScript function that reverses a string and returns it. var str = 'Lorem ipsum dolor sit amet.'; String.prototype.reverseStr = function () { var string = String(this), tempStr = string.split(''); tempStr = tempStr.reverse();/*w ww. j a v a2s. c o m*/ return tempStr.join(''); }; console.log(str.reverseStr());
//Just like you can add methods to your own constructor, you can also add methods and properties to built in classes in JavaScript like Arrays and Objects. //Add a reverse method to the String 'class' so that every instance of String can call reverse and reverse itself. String.prototype.reverseStr = function() { return this.split("").reverse().join(""); }; var name = 'Dustin'; name.reverseStr();/*from ww w .j av a 2 s . c o m*/