logo

Reversed Strings

Complete the solution so that it reverses the string passed into it.
'world' => 'dlrow'
'word' => 'drow'

My Code:

function solution(str) {
                        let input = [];
                        let reverse = [];
                        for (var i = 0; i < str.length; i++) {
                          input.push(str[i]);
                        }
                        for (var x = input.length - 1; x >= 0; x--) {
                          reverse.push(input[x]);
                        }
                        return reverse.join("");
                      }
                    

And that's the best solution among all users:

function solution(str){
                        return str.split('').reverse().join('');  
                      }      
                    

It's just awesome that my multiline code can be written in only two lines. I learned two new methods: .split() and .reverse().