* fix: restructure certifications guide articles * fix: added 3 dashes line before prob expl * fix: added 3 dashes line before hints * fix: added 3 dashes line before solutions
2.9 KiB
title
title |
---|
Reverse a String |
Reverse a String
Hints
Hint 1
We need to take the string and reverse it, so if it originally reads 'hello', it will now read 'olleh'. We will need to split the string, and therefore we will be working with Arrays as well.
Hint 2
Start by splitting the string by characters.
Relevant Links
Hint 3
Look up the built in function to reverse a string.
Relevant Links
Hint 4
Do not forget to join the characters back together after you reverse them.
Relevant Links
Solutions
Solution 1 (Click to Show/Hide)
function reverseString(str) {
return str
.split("")
.reverse()
.join("");
}
Code Explanation
-
Our goal is to take the input,
str
, and return it in reverse. Our first step is to split the string by characters usingsplit('')
. Notice that we don't leave anything in between the single quotes, this tells the function to split the string by each character. -
Using the
split()
function will turn our string into an array of characters, keep that in mind as we move forward. -
Next we chain the
reverse()
function, which takes our array of characters and reverses them. -
Finally, we chain
join('')
to put our characters back together into a string. Notice once again that we left no spaces in the argument for join, this makes sure that the array of characters is joined back together by each character.
Relevant Links
Solution 2 (Click to Show/Hide)
function reverseString(str) {
for (var reversedStr = "", i = str.length - 1; i >= 0; i--) {
reversedStr += str[i];
}
return reversedStr;
}
Code Explanation
-
Starting at the last character of the string passed to the function, you build a new string
reversedStr
fromstr
. -
During each iteration of the
for
loop,reversedStr
gets concatenated with itself and the current character. -
Finally, you return the final value of
reversedStr
.