Another Way To Pass? Reverse a String

Another Way To Pass? Reverse a String
0

#1

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/


#2

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!”


#3

The solutions in the guide are by no means the only solutions. I can probably think of 10 different ways to solve this problem. Don’t worry that your solution looks different. Instead, study the other solutions and understand the algorithm and syntax being used.

FYI - I have blurred out your solution, because we don’t want to spoil it for others still working on this challenge who might come across your thread.

As an extra challenge, see if you can modify your existing solution to not use arrays at all.

Hint: Your reverse variable would initialize as a blank string.


#4

Thank you @snowmonkey and @RandellDawson . 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.


#5

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