Reverse: String


Directions

  • Given a string, return a new string with the reversed order of characters.
  • examples:
reverse('apple') //--> elppa
reverse('hello') //--> olleh
reverse('Greetings!') //--> !sgniteerG

Solutions

JS

My Solution(s)

function reverse(str) {
    return str.split('').reduce((result, char) => {
        return character + reversed;
    }, '')
}

// as one-liner:
const reverse = (str) => str.split('').reduce((result, char) => char + result, '');

SG Solution 1 (w/ reverse())

function reverse(str) {
    return str.split('').reverse().join('');
}

SG Solution 2 (w/o reverse())

function reverse(str) {
    let reversed = '';

    for (const character of str) {
        reversed = character + reversed;
    }

    return reversed;
}
Made with Gatsby G Logo