We need to take the string and reverse it, so if it originally reads ‘hello’, it will now read ‘olleh’.
One possible way to solve this challenge is by creating a new string (initializing it to a blank string “”) and then iterating the string starting from the last character through the first character and the concatenating each character to the new string. After iterating through all the characters in the string, you return the new string.
Solutions
Solution 1 (Click to Show/Hide)
function reverseString(str) {
let reversedStr = "";
for (let 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 from str.
During each iteration of the for loop, reversedStr gets concatenated with itself and the current character.
Finally, you return the final value of reversedStr.
Solution 2 (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 using split(''). 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 chainjoin('') 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.
Here is mine. Maybe is more clear for beginners. You can first split, hit the ENTER-see result, then
reverse it and at the end joint it together. On every step you can see you result on board.
I am a beginner to this world of coding, but I was pretty proud of what I came up with. I created a variable rev to hold the split array, then reverse it, then join it together again in the return statement.
function reverseString(str) {
var rev = str.split("");
rev.reverse();
return str = rev.join("");
}
function reverseString(str) {
var array = [];
array = str.split('');
array = array.reverse();
str = array.join('');
return str;
}
reverseString("hello");
also, try using cloud9 ide for debugging and console logging https://ide.c9.io/ or https://jsbin.com/ , freecodecamp doesn’t provide too much info on debugging
function reverseString(str) {
var rev = "";
str.split("");
for(var i = str.length -1; i >= 0; i--){
rev += str[i];
}
return rev;
}
reverseString("hello");
So, basically, I created the empty string variable: rev, split str into an array, looped through str from back to front and put each letter into my rev variable.
I played around with solutions and discovered that using the JavaScript methods above is actually a really code heavy solution. The methods hide the work that’s going on in the background to make them happen. I ended up creating this solution instead. Even though it looks a little longer it’s actually a lot shorter than the solution posted above
No need to run all of those methods twice just return your new variable, or don’t declare a new variable and just use your existing return statement. Also, the methods look fancy and chaining them is fun, but @Becca941 is %100 correct. From what I understand, methods are fast, easy, and super useful, but often not the most efficient solution as far as processing time. (Something about big O notation??)
You only need to split, reverse, and join once. You are doing it twice, which is returning the same thing as with less code. Good work though, just some ‘useless’ code in there, meaning it is pointless because it’s already being done.
Here is what I mean:
function reverseString(str) {
return str.split(’ ‘).reverse().join(’ ');
}
reverseString(“Greetings from Earth”);
or you can do
function reverseString(str) {
var strReverse = str.split(’ ‘).reverse().join(’ ');
return strReverse;
}
reverseString(“Greetings from Earth”);
Nevermind, got it. This is intermediate:
function reverseString(str) {
var splitted = str.split("");
var reversed = splitted.reverse();
var joined = reversed.join("");
return joined;
}