Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of rootulp

rootulp's solution

to Reverse String in the TypeScript Track

Published at Mar 10 2019 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

Reverse a string

For example: input: "cool" output: "looc"

Setup

Go through the setup instructions for TypeScript to install the necessary dependencies:

http://exercism.io/languages/typescript

Requirements

Install assignment dependencies:

$ yarn install

Making the test suite pass

Execute the tests with:

$ yarn test

Source

Introductory challenge to reverse an input string https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

reverse-string.test.ts

import ReverseString from './reverse-string'

describe('Reverse String', () => {
    it('an empty string', () => {
        const expected = ''
        expect(ReverseString.reverse('')).toEqual(expected)
    })

    xit('a word', () => {
        const expected = 'tobor'
        expect(ReverseString.reverse('robot')).toEqual(expected)
    })

    xit('a capitalized word', () => {
        const expected = 'nemaR'
        expect(ReverseString.reverse('Ramen')).toEqual(expected)
    })

    xit('a sentence with punctuation', () => {
        const expected = `!yrgnuh m'I`
        expect(ReverseString.reverse(`I'm hungry!`)).toEqual(expected)
    })

    xit('a palindrome', () => {
        const expected = 'racecar'
        expect(ReverseString.reverse('racecar')).toEqual(expected)
    })
})
class ReverseString {
    static reverse(input: String): String {
        const reversed = input.split("").reverse().join("")
        return reversed
    }
}

export default ReverseString

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other peopleโ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?