TheOdinProject/javascript-exercises

03_reverseString: Add alternative solution

jwsoh07 opened this issue · 8 comments

Complete the following REQUIRED checkboxes:

  • I have thoroughly read and understand The Odin Project Contributing Guide
  • The title of this issue follows the location for request: brief description of request format, e.g. Exercises: Add exercise on XYZ

The following checkbox is OPTIONAL:

  • I would like to be assigned this issue to work on it

1. Description of the Feature Request:

Hello there!
I think it will be useful for TOP learners to see an alternative solution for this exercise that is, in my opinion, quite important.
The proposal is to add another solution, perhaps in the same file as reverseString-solution.js or in a new file.

The solution I am proposing is this.

const reverseString = function(input) {
    let result = "";
    for (let i = input.length - 1; i >= 0; i--) {
        result += input[i];
    }
    return result;
};

The benefit is that the learners can get to learn a style of algorithm, that is closer to what they will eventually may need to learn from data structures and algorithms (if they have not yet!). As there are many ways to solve the problem, I think it's also good that students can get exposure to different solutions and learn about the difference in their runtime as well.

@TheOdinProject/javascript Can someone take a look please.

const reverseString=(str)=> {
if (str === "")
return "";
else
return reverseString(str.substr(1)) + str.charAt(0);
}
reverseString("hello");

const reverseString = function (str) {
if (str === "") {
return "";
} else {
let reverse = str.slice(-1);
for (let i = 1; i < str.length; i++) {
reverse += str.slice(-(i + 1), -i);
}
return reverse;
}
};

I wouldn't really be opposed to adding an alternative solution as a comment in the solution file (typically that's what we've been doing), but we also can't add every possible solution to an exercise. Here we already have 3 slightly different solutions, but we really can't include them all. At a certain point providing too many alternative solutions may either a) confuse users who don't understand that concept yet, or b) send users down a rabbit hole of looking into how those alternative solutions work.

@jwsoh07 if you'd like to put a PR up to add your solution as a comment to the solution file, I've assigned you, though. In this case since the exercise is assigned in a lesson that covers loops it could make sense to provide this alternative.

@thatblindgeye thank you!
I have worked on it and created a pull request #423

@thatblindgeye I have created another PR #427 to replace the previous one.

@jwsoh07 are you still interested in working on this?

@thatblindgeye I will not add my solution because I think it's better that the solution should be more readable. Let's close this :)