String Reverse how to

Tell us what’s happening:

Your code so far

function reverseString(str) {
  return str;
}

reverseString("hello");
var reverse = reverseString.reverse;

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36.

Link to the challenge:
https://www.freecodecamp.org/challenges/reverse-a-string

Did you have a question?

I did but I figured it out, I tried to reverse the word instead of splitting, reversing, and rejoining.

return str.split("").reverse().join("");

5 Likes

I’m sitting here pushing the single letters into arrays with a for-loop while you solve this in 1 row…

1 Like

Different approaches.
I prefer the 3rd way :thinking:

function reverseString(str) {
    let tempArr = [];
    let result = '';

    // for(let i = 0; i < str.length; i++) {
    //     tempArr.push(str[i]);
    // }
    // let length = tempArr.length;
    // for(let i = 0; i< length; i++){
    //     result += tempArr.pop();
    // }

    // return str.split("").reverse().join("");

    for(let i = str.length-1; i >= 0; i--){
        result += str[i];
    }
    return result;
  }
3 Likes

On my first Attemp i tried to solve it with a for loop.
but i forgot the += and tried another way to solve it.

i google for array functions and found the .reverse() function and solved it.

but i like the for-loop solving better.

do you know how i can make the word loop infinite, till something is happen?