String Reverse how to

String Reverse how to
0.0 0

#1

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:


#2

Did you have a question?


#3

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


#4

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


#5

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


#6

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;
  }