Reverse string Algorithm Script

Hi guys,

It seems to be passing in the console…not sure why it’s only passing one test.

Would love some advice.

Cheers guys!

  **Your code so far**

function reverseString(str) {
let string = " ";
for (let i = str.length - 1; i >= 0; i--) {
  string += str[i];
}
return string;
}

console.log(reverseString("hello"));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36

Challenge: Reverse a String

Link to the challenge:

Is this the string you want to start with?

1 Like

Yes? An empty string.

That isn’t empty though… It has a space inside it.

3 Likes

Blooming eck, just passed with one delete of a button haha.
Thanks once again Jeremy. Won’t make that mistake again.

2 Likes

Is not it better? :roll_eyes:

const reverseString = str => [...str].reverse().join('');
console.log(reverseString('ABC'));

I have added spoiler tags around your code since it is a working solution and we don’t want to spoil it for those who have not worked on this challenge yet.

There are many ways to do this challenge.
Using the reverse method is a popular approach but using a for loop like the OP just did is a perfectly valid approach to.

As long as the OP understands the logic of the problem and understands what went wrong with their initial approach, that is the real goal of these challenges. :grinning:

1 Like

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