Foundation path lesson loops and array: Excercises : 07_reverseString - Improve Hint/ Solution to better understand loop.
Prerequisites
- [x] I have thoroughly read and understand The Odin Project Contributing Guide
- [x] I have searched the existing issues to ensure this improvement hasn't been suggested before (please have a look through our open issues list to make sure)
- [x] The title of this issue follows the
<Location of the improvement>: <Brief description of the improvement>format, e.g.Exercises: Add exercise on XYZ - [x] (Optional) I am interested in working on this issue and would like to be assigned to it
Improvement Description
In Foundation path lesson Loops and Arrays 's Assignment number 3 we have 07_reverseString . Hint section of the excercise need some improvement because hint section specify that :
Strings in JavaScript cannot be reversed directly so you're going to have to split it into something else first.. do the reversal and then join it back together into a string.
But I think better way as a beginner to solve this problem and building logic will be to iterate through the string from the last element towards first and concatenate it in new string and return new string, because we're in loops and array lesson, so we could use loops without converting string to array and array back to string.
const reverseString = function (string) {
let str = "";
for (let i = string.length - 1; i >= 0; i--) {
str += string[i];
}
return str;
};
Acceptance Criteria
-[] Update hint to either saying you can also contain it into different string and then use loop to reverse it so we're basically not working with array for reversing string or -[] Update the solution to looping one.
(Optional) Additional Comments
No response
Thanks for raising this. I don't think we need to push towards loops specifically, especially given the lesson is "Loops and Arrays", and the current solution deals with arrays.
That being said, I do think the current hint is a little too prescriptive, and I'm thinking perhaps it ought to have something a little more general. What do you think about something like:
Strings in JavaScript cannot be directly reversed (no built-in
reversestring method). There are many ways you can go about this, so think about what you've been introduced to in the lessons so far.
I also wouldn't be opposed to adding an alternative loop-based solution in the solution file, something like (must be commented out of course):
// Another approach could be to loop over the string in reverse direction to construct a new one
// const reverseString = function (string) {
// let reversedString = '';
// for (let i = string.length - 1; i >= 0; i--) {
// reversedString += string[i];
// }
// return reversedString;
// };
What do you think?
Yes making hint more general will help people to think about solutions in general way instead of constraining them to one solution which will make them think that to reverse string we have to change it into array and back to string, besides adding commented out solution can make people to think about solution in different ways which they may have never thought. So we can update hint like you outlined above:
Strings in JavaScript cannot be directly reversed (because it is immutable so can't really reverse it in place) (no built-in reverse string method). There are many ways you can go about this, so think about what you've been introduced to in the lessons so far.
All yours