Reverse String-v6

Reverse String-v6

Reverse String can be defined as an operation in which the original string which the user gives is modified in such a way that the characters in it are arranged in a reverse manner starting from the last character to the first character, thus by forming a new string which will be the exact reverse of the original

Example:
var str = new String(‘emir’).reverse(); => rime
console.log(str) => rime

String.prototype.reverse = function() {
  var newStr = '';
  for(let i = this.length -1; i >= 0; i--) {
    newStr += this[i]
  }
  return newStr;
}

var str = new String('emir').reverse(); => rime
console.log(str) => rime

We create a String Prototype.reverse to be equal a function and we create a variable newStr = ‘’

We use for loop to go from i to the end of the string; this can be done with this.length -1
Then we loop until i is bigger or equal to 0
In the end we decremenet by 1 with i–


Inside the loop we use newStr += this[i] it means newStr is appending to the current i

At the end of the Prototype we return newStr

Outside the loop we create a variable str = new String(‘emir’).reverse();
Variable str is to hold the value of the new instance of String with parameter of string ‘emir’ (my name) and we chain .reverse(); to reverse it

comments powered by Disqus