Another Way To Pass? Reverse a String

Tell us what’s happening:
This isn’t a post to ask for help as this solution works. My question is, after looking at the “Get A Hint” feature to check my reasoning, another way was suggested.

Now, to the author’s credit, it works and it’s much more elegant and concise. However, from a student’s perspective, it doesn’t make sense as the solution requires knowledge of methods that were not covered. Whereas, my solution (albeit in a clunkier way) not only passes the challenge, but it more iterative of what a complete beginner using no other resources might come up with based on the knowledge covered. Any chances of having such solutions given for these types of problems?

It’s already done quite often, but I know I am not alone in feeling discouraged when the the challenge asks a question and the answer is given in a completely different way.

Your code so far


function reverseString(str) {
  let reverse = [];
  for (let i = str.length; i >= 0; i--){
    reverse.push(str[i])    
    } 
  console.log(reverse) //returns ,h,t,r,a,E, ,m,o,r,f, ,s,g,n,i,t,e,e,r,G
  return reverse.join("")
} 

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/reverse-a-string/

1 Like

I’ve been a developer for many many years, and when I first ran these challenges, I tried them with the mindset of someone who has no experience – simply went with what we’d been given up to that lesson. And it works.

But I’ve also, at this point, revisited MANY of the JS challenges with an eye toward “how would I, as a sophisticated developer, rewrite that to be more efficient or elegant?” There is no limit to how many times you can revisit a challenge, and there is really no limit to how many different ways you can posit solutions for each.

In the solutions, there are often three given: a beginner, an intermediate and an advanced solution. It isn’t intended as a discouragement. When you get to a solution any way you do, you’ve succeeded! But there are other ways, and as your proficiency increases, you may look at the intermediate solutions and say, “Oh, that makes sense! So easy!”

1 Like

Thank you @snowmonkey and @camperextraordinaire . Sometimes I second guess my answers because they look differently, but I suppose it’s all part of the learning curve. And thanks for the extra challenge idea.

It’s been a few months since I went through this, but I think I did something similar to you initially, and when I clicked on the hint, I discovered MDN - first result in search

In the post linked below, I used methods covered in preceding lessons.

Hope this contributes to what you are looking for.