DEV Community

Viren B
Viren B

Posted on • Updated on • Originally published at virenb.cc

FCC Algorithm Challenges / Reverse a String

Original post can also found at https://virenb.cc/fcc-002-reverse-string

Reverse a String

function reverseString(str) {
  return str;
}

reverseString("hello");

/// TESTS
reverseString("hello") should return a string.
reverseString("hello") should become "olleh".
reverseString("Howdy") should become "ydwoH".
reverseString("Greetings from Earth") should return 
"htraE morf sgniteerG".

Above is the starter code provided for the challenge, "Reverse a String".

Our goal is to write a function that to take the input of a string and reverse the string (the tests show some examples). Let's think this through. Here is how I would aim to solve this problem.

Method

  1. Read (!)

    • Read the instructions first. Make sure you understand what it being asked of you.
    • Read the starter code. Go line by line, just making sure you know what is going on initially.
    • Have a look at the tests. If the problem isn't clear to you, looking at the tests might give you an inclination of what kind of output you should aim for (i.e. instead of returning an array, maybe the problem is only asking for an index within the array).
  2. Think & Write

    Now that you've read through the instructions, starter code, and tests, it's time to analyze what to do and in what order. It may be handy to write out pseudocode.

  3. Code

    Once you've thought about what you'd like to do, and in what order, start to convert your pseudocode into JavaScript code.

There's been too many times where I've tried to jump write into writing the code without thinking it through (in projects and coding challenges). That will leave you testing it way too many times, creating unnecessary variables, and running into more problems then you need to handle. If I try to follow the above method, it leaves me with a more clear mind of what I'm doing and hopefully writing some DRY code. Let's try to solve this problem now.

Thoughts

  • We're given a string and we have to reverse it and return a string
  • The string data type has a few built in, String.length. It will provide us with the number of characters in the string. We would need length if we decided to use a loop
  • There are some other handy methods and properties for strings and arrays (reverse for array)
  • We can separate our string to become an array with the split() method. We use split('') so each character is separated into their own index in the array
  • Running split() method on the string will give an array, then we can call reverse(), which will reverse the order of the array (backwards to forwards)
  • Once it is reversed, there is a join() method that will convert the array back to a string, which is the data type we need to return
  • We have to call join('') with quotations otherwise value is returned as "o,l,l,e,h" not "olleh"

Solution

Some Pseudocode

function reverseString(str) {
    // we have an argument of str, a string data type
    split it into an array
    reverse the array
    convert array back to string
    return string value
}

[SPOILER: SOLUTION TO CODE BELOW]

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

Alternative Solutions

function reverseString(str) {
  let strArray = [];
  for (let i = str.length; i >= 0; i--) {
    strArray.push(str[i]);
  }
  return strArray.join('');
}

The alternative solution is a little more verbose. We declare a variable and then loop through the string array.

Links & Resources

Reverse a String Challenge on FCC

FreeCodeCamp

Donate to FCC!

String.length on MDN

Array.join() on MDN

Array.reverse() on MDN

String.split() on MDN

Solution on my GitHub

Thank you for reading!

Top comments (0)