Reverse String-v8

Reverse String-v8

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

function reverse(value) {
  let result = ''
  
  let array = value.split('').length
  for (let i =0;i<array;i++){

    result = value[i] + result
  }
  return result;
}
console.log(reverse('emir')) => rime

We create a function called reverse and we create parameter value and variable result=’’

Then we created another variable array to be equal to value.split(‘’).length
.split(‘’) will split the string into an array and .length checks the lenght

We loop from 0 until the end of the newly formed array called array and we increment by 1

result = value[i] + result

The empty initially variable result will take the first r and the rest of the string

In the end we return result

comments powered by Disqus