Basic Algorithm Scripting: Reverse a String

Just did this challenge. I made a long code using the methods and loops that I had already learned via FCC up to this point (starting from the top of the site and working down).

function reverseString(str) {
  let newArr = [];
  let newStr = "";
  let myRegex = /./ig;
  let strArr = str.match(myRegex);
  for(let i = strArr.length - 1; i >= 0; i--){
    let popped = strArr.pop();
  for(let j = 0; j < newArr.length; j++){
    newStr += newArr[j];
  return newStr;

It worked, it returned, for example:

console.log(reverseString("Greetings from Earth"));
// returned htraE morf sgniteerG

I looked at the hints, because I wanted to compare my answer with that answer.

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

And I hadn’t learned about any of the three methods used here up to this point. It seemed kind of off that, if we’re supposed to use methods that we had learned up to this point, that the answer would be conjoining three methods that hadn’t appeared yet.

Just to add, I am using this to learn about those three methods :slight_smile:

Solving a problem isn’t always the exercise here. It’s being able to follow the logic. So I would argue learning both ways are beneficial.

At one point most of the challenge “hints” had a couple different possible solutions, including one “advanced” one that might use methods or approaches that haven’t been taught yet in FCC.

As the curriculum has grown and changed, it may no longer be true that all challenge hint pages have multiple solutions. Remember that these are all written by volunteers. If a hint page is missing a basic solution, feel free to contribute one!

Some people come to FCC with prior experience. Some people like to repeat challenges after they’ve learned more. And some people like to take the initiative and research what methods might be available to help them solve a problem (eventually this will be necessary to complete projects).

The non-algorithm part of the curriculum holds your hand all the time, it spoon feeds information. The algorithm sections do not, you have to figure out how to solve each one (in any way you wish). Note, you do need to to get used to reading the documentation on MDN (and looking things up on Google or Stack Overflow or wherever else you find that is useful for you) at this point.

It shouldn’t make much difference what you already know or not to be able to use these sections to learn and practice. Any given problem can be solved in multiple different ways (and generally in any programming language!). The only thing your solution needs to do is take the provided input and generate the expected output. You’ve seen these two solutions: now try not using regex (is it really doing anything there?). Or try using a for…of loop instead of a for loop. Or maybe you could try writing a solution that doesn’t use loops or any of the string or array functions. Etc.

Thanks for the replies everyone!

It is nice to know that this is real world practice, not knowing something and searching via MDN or Stack Overflow. This problem did teach me to search for different methods rather than figure out how to incorporate a loop for every problem haha.

The regex I put in there to chop up the string into pieces so that I can pop each one off. But in the short time since, I have found a few other methods to do something similar.

My job consists of spending a lot of time searching docs and discussion threads and articles and sample code.

Hi ~ It’s my solution
Even though I did not use multiple functions, only the results …

function reverseString(str) {
  let con = "";
  for (let i = str.length-1; i >=0; i--) {
    con += str[i];    
  return con;
let rev = reverseString("Greetings from Earth");

Your solution is the fastest., but readability > speed.

The only time readability < speed is if your job requires it or you’re working with a very large dataset.


It is still a beginner and I have only thought in other ways.
Thanks for the advice.:sweat_smile:

So, recently, I encountered a REALLY annoying challenge on Codewars - recreate Array.prototype.reverse in under 30 characters. Turns out, to pull it off, you had to leave of the const, let, or var (pare it back to barest bones).

However, that solution led to a very short, though kind of complex, string reversal idea:

Codewars-based reversal solution
 * So, in the parameters, we convert the string to an array using spread.
 *   Then, we map that array, using pop as the callback function. We also
 *   need to use's optional parameter, thisArg, to define an
 *   array we will mutate, leaving the original untouched.
 *   Finally, we simply rejoin the array, and return the result.
const reverseString = ([...s])=>, [...s]).join('');

IMO, pretty painful, but concise and functional. So there’s that. :wink:

That sort of thing is why I lost interest in Codewars pretty quickly.

1 Like

BAhahahaha! I’m with you. It was interesting to see, but it was simply annoying, and for a high-level challenge, it fostered poor code.

Thank goodness for FCC.