-2

This is a very specific question and I want to reverse a string in this way however I don't know how to go about it.

What i want to do is take a word lets say 'hello'. olleh

and take the first and last letters and output 'oellh' then doing the same thing for the next two characters so 'e' and 'l' which would then output 'olleh'.

So to summaries this I need to reverse the first and last character and then the same thing for the second characters until i get to the middle character.

This must use a for loop.

        reverse('hello');

    function reverse(string) {

        var character = [];

        for (var i = string.length -1; i >= 0; i--) {
            character.push(string[i]);
        }

        console.log(character.join(""));

    }

Let me know if this needs further explanation

Max Lynn
  • 1,738
  • 6
  • 22
  • 35
  • My apologies forgot to add. – Max Lynn Nov 08 '15 at 19:42
  • your code is working. whats the problem? – rajuGT Nov 08 '15 at 19:44
  • Can i ask why this has been down voted? – Max Lynn Nov 08 '15 at 19:44
  • There isn't a problem with my code there is a problem with how it is reversed. This does reverse my string however I want to do it by taking the first and last character and then the second and second to last character until it gets to the middle. This is a question on simplifying my code. – Max Lynn Nov 08 '15 at 19:45
  • 1
    Then you should show your attempt to do it that way. SO is not a place to ask for finished code, but to help with your implementation that does not work. – t.niese Nov 08 '15 at 19:48
  • I'm asking for guidance on how to do this as I can't find a solution. I am not asking for finished code. – Max Lynn Nov 08 '15 at 19:49
  • So are you trying to reverse it without creating an array? – Blue Boy Nov 08 '15 at 19:49
  • Yes, however I'll need to have one variable to store the characters – Max Lynn Nov 08 '15 at 19:51
  • 2
    One way would be to have a `for` loop from `0` to the the half length of you string and then exchange the index `i` with the index `length-1-i`. – t.niese Nov 08 '15 at 19:52
  • Not sure I understand. is i being the first character and the length-1-i being the last? – Max Lynn Nov 08 '15 at 19:57
  • Well the `i` is the same as in the code you provided. And what is the letter at index `length-1`? And what might the `length-1` minus the current `i` be? I would suggest to try it with a loop and a debugger to see what they are. – t.niese Nov 08 '15 at 20:07
  • Thanks for your time but it appears this isn't as simple to do as i thought it would be – Max Lynn Nov 08 '15 at 20:08
  • Possible duplicate of [How do you reverse a string in place in JavaScript?](http://stackoverflow.com/questions/958908/how-do-you-reverse-a-string-in-place-in-javascript) – t.niese Nov 08 '15 at 20:15

2 Answers2

1

This might do the trick:

function replaceAt(string, index, character){
    return string.substr(0, index) + character + string.substr(index+character.length);
}

function reverse(string) {
    var len = string.length;
    len = len/2;
    var s = string;

    for (var i = 0; i < len ; i++)
    {
        var m = s[string.length-i-1];
        var k = s[i];
        s = replaceAt(s,i, m);
        s = replaceAt(s, string.length-i-1, k);       
    }
       return s;

}
Ikbel
  • 7,721
  • 3
  • 34
  • 45
  • 1
    You should never extend the prototype of core objects - except for polyfills -, because such code might break future code if a `replaceAt` function with a different signature is introduced in the standard. – t.niese Nov 08 '15 at 20:21
  • This is good, but there should be a way to do this with one variable. – Max Lynn Nov 08 '15 at 20:50
  • I don't think you can do that with just one variable. you have to use temporary variables to be able to reverse them. – Ikbel Nov 08 '15 at 21:10
0

You can easily reverse a string doing:

"hello".split("").reverse().join("")
Elieder
  • 141
  • 1
  • 7