Reverse String-v7

Reverse String-v7

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(s){
    return [...s].reverse().join("");
}

console.log(reverse('emir')) rime

We create a function called reverse and we create parameter s

We return […s].reverse().join(“”)

We split the string to an array with spread operator with […s]

We chain .reverse() to reverse the order of the newly formed array

We chain .join(“”) to concat the reversed array into string

comments powered by Disqus