-1

I'm going through tutorials to code (I'm fairly new at this), and this particular exercise is racking my brain. Here are the parameters:

Reverse the provided string. You may need to turn the string into an array before you can reverse it. Your result must be a string.

and here is the code I'm given to start with:

function reverseString(str) {
  return str;
}
reverseString('hello');


expect(reverseString('hello')).to.be.a('String');

expect(reverseString('hello')).to.equal('olleh');expected 'hello' to equal 'olleh'

expect(reverseString('Howdy')).to.equal('ydwoH');expected 'Howdy' to equal 'ydwoH'

expect(reverseString('Greetings from Earth')).to.equal('htraE morf sgniteerG');expected 'Greetings from Earth' to equal 'htraE morf sgniteerG'

Any suggestions out there on how to accomplish this?

** Edit: I figured out what my issue was. The particular IDE of the tutorial site made it confusing. Apparently I was meant to hit one of the objectives listed (not all of them in one script as I previously thought). This was accomplished by return str.split( '' ).reverse( ).join( '' );. The parameters for the split and join methods were a little confusing at first as well. Most online tutorials of this method use splitting words as an example, so I didn't realize going from

" " to ""

would change the process from reversing words to reversing letters.

mason
  • 31,774
  • 10
  • 77
  • 121
Kazem Behbahani
  • 31
  • 1
  • 1
  • 3

5 Answers5

10

Arrays have a method called reverse( ). The tutorial is hinting at using this.

To convert a string into an array of characters (in reality they're just single character strings), you can use the method split( ) with an empty string as the delimiter.

In order to convert the array back into a string, you can use the method join( ) again, with an empty string as the argument.

Using these concepts, you'll find a common solution to reversing a string.

function reverseString(str) {
    return str.split( '' ).reverse( ).join( '' );
}
David Thomas
  • 249,100
  • 51
  • 377
  • 410
Austin Brunkhorst
  • 20,704
  • 6
  • 47
  • 61
2

Pretty manual way to accomplish this

var j = 'Ramarao';
var k = j.split('');


var reversedArr = []
for(var i = k.length; i >= 0; i++) {
    reversedArr.push(k[i])
}

var reversedStr = reversedArr.join('')
console.log(reversedStr)
Community
  • 1
  • 1
Brant
  • 1,764
  • 11
  • 18
1

You can read more here: http://eddmann.com/posts/ten-ways-to-reverse-a-string-in-javascript/

function reverse(s) {
  var o = '';
  for (var i = s.length - 1; i >= 0; i--)
    o += s[i];
  return o;
}
Brian Lewis
  • 5,739
  • 1
  • 21
  • 28
0

A string is an array of characters, so you can use the reverse function on the array to reverse it and then return it:

function reverseString(str) {
    return str.split('').reverse().join('');
}
Gaston Sanchez
  • 1,181
  • 6
  • 13
  • Note: the `split` part is what turns the `string` into a `char[]` (an array) and the `join` part turns it back into a `string`. – Alex Booker Aug 03 '15 at 18:03
0
var reversedStr = normalStr.split("").reverse().join("");
Joel Banzatto
  • 147
  • 2
  • 9