Basic Algorithm Scripting. Reverse a String

Hi everyone,

There is a lesson freecodecamp

I completed it on my own, but when I looked into solutions I’ve found interesting and neat solution:

function reverseString(str) {
  return str
    .split("")
    .reverse()
    .join("");
}

Question: why is RETURN STR at the top, and “actions” are after it… I kinda understand what each of the following lines do but I just don’t understand why the function returns str before splitting, reversing and joining it…

Hi,

It doesn’t return str before it does those “actions”, it ignores white space, in this instance new line it is same as str.split("").reverse().join("").

2 Likes

function reverseString(str) {

let temparr=;

let revStr="";

for(let i=0; i<str.length; i++){

temparr[i] = str[str.length-(i+1)];

revStr += temparr[i];

}

return revStr;

}

console.log(reverseString(“hello”));