Basic Algorithm Scripting - Reverse a String

Tell us what’s happening:
what is the issue with this code please help me out.

  **Your code so far**
function reverseString(str) {
let arr = [...str];
   arr.reverse();
   let str1 = arr.toString();
      str1.replace(/\s/g, ""); 

console.log(str1);
}

reverseString("hello");
  **Your browser information:**

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

Challenge: Basic Algorithm Scripting - Reverse a String

Link to the challenge:

You shouldn’t use toString(). Instead, you should use .join(""), for this will convert the array to a string.

Another thing is that you aren’t returning anything from this function. You should write a return statement after console.log(str1).

console.log(str1), I just used to see how the code is looking but does not the str1.replace(/\s/g, “”); should do the trick to replace (,) hence joining the string.

Actually i am confused with {str1.replace(/\s/g, “”);} if i remove this part of code i am getting o,l,l,e,h as a output

i have also used this part of code {str1.replace(/\s/g, “”);} as, {str1.replace(/,/g, “”);} but still the same result

Look up the replace method and what it returns. The code str1.replace(/,/g, "") is doing what you want, but you are not saving the results anywhere and it is getting tossed out.

Thankyou so much, I got it by assigning {str1.replace(/,/g, “”)} to a different variable.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.