Given a string, return a new string with the reversed order of characters
reverse('apple') === 'leppa'
reverse('hello') === 'olleh'
reverse('Greetings!') === '!sgniteerG'
Three possible solutions:
for...of
loopreduce
function reverse(str) {
return str.split("").reverse().join("");
}
for...of
loop¶function reverse(str) {
let reversed = "";
for (let character of str){ /* try not to use for loop, use for...of loop instead! */
reversed = character + reversed;
}
return reversed;
}
reduce()
¶function reverse(str){
return str.split("").reduce((rev, char) =>
char + rev
,"");
}
reverse('apple')
reverse('hello')
reverse('Greetings!')