So i have reached the section Basic Algorithm Scripting and my first question is on Reverse a String.
prob the cleanest version i found on this .
return str.split("").reverse().join("");
It’s ok to approach this however you like
My method for solving problems with the algorithms is to think about what I have as a piece of data, in this case a string, and then looking up what methods I can use on that data type.
So in your example split() is an interesting one because it gives you a new data type (an array).
So then you can look up the methods on that, like reverse() and join().
Do be discouraged if you need to look things up though. The algorithm challenges are designed to stretch your problem solving reasoning and introduce you to new methods. Look up code if you need to, but always find out what it means and why it works
Happy coding!
My first solution was:
return [...str].reverse().reduce( (a,b) => a + b );
Thanks for ‘join()’! Now it is:
return [...str].reverse().join("");