Skip to content

Commit c52633d

Browse files
authored
reverseString: Make hint more generic (#578)
Provide alternative solution that also draws from the Loops and Arrays lesson.
1 parent 1c4381c commit c52633d

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

07_reverseString/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ reverseString('hello there') // returns 'ereht olleh'
77
```
88

99
## Hints
10-
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.
10+
Strings in JavaScript are immutable and, therefore, cannot be reversed directly in place. While there is no built-in method for this, several alternative approaches can be used, drawing on the concepts you've been introduced to in the lessons.
11+

07_reverseString/solution/reverseString-solution.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,17 @@ const reverseString = function (string) {
33
};
44

55
module.exports = reverseString;
6+
7+
8+
9+
// Another approach could be to loop over the string in reverse direction to construct a new one
10+
/*
11+
const reverseString = function (string) {
12+
let reversedString = "";
13+
14+
for (let i = string.length -1; i >= 0; i--) {
15+
reversedString += string[i];
16+
}
17+
return reversedString;
18+
};
19+
*/

0 commit comments

Comments
 (0)