Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have to reverse a string in JavaScript and cannot use the built in reverse() function. It does not seem efficient to have to create two arrays but is there a better way?

function reverseString(str) {
  newarr = str.split("");
  result = [];
  x = newarr.length;
  for (i = x; i > -1; i--) {
    result.push(newarr[i]);
  }
  str = result.join("");
  return str;
}

reverseString("hello");
share|improve this question
up vote 7 down vote accepted
  1. One array to rule them all

    You do not need to create two arrays; one is enough. Simply swap slots until you've reached the "middle" of the array.

  2. Declare your variables

    It is better to use the var keyword to define your variables. Otherwise, they are declared on the top-level scope, i.e; they become global, and may be accessed and modified by other functions.

  3. Why not use reverse()?

    You do not say why you cannot use built-in functions. If you happen to have some base code which overrides native functions, then some groaning and roaring is in order.

function reverse(str) {
  var chars = str.split("");
  var length = chars.length;
  var half = length / 2;
  for (var ii = 0; ii < half; ii++) {
    var temp = chars[ii];
    var mirror = length - ii - 1;
    chars[ii] = chars[mirror];
    chars[mirror] = temp;
  }
  return chars.join("");
}

console.log(reverse("abcd"));
console.log(reverse("abcde"));

share|improve this answer
2  
Don't you like a single i as a variable? ;-) – t3chb0t Jul 4 at 8:29
    
@t3chb0t It's just a thing of the past, which I keep because... I'm a romantic person. Using "ii" rather than "i" would help telling it's not the figure "1", as well as searching loop counters in basic text editors. Nowadays, it's not so good an idea to use it, I guess. – Elegie Jul 4 at 11:10
    
Also strict mode would prevent things like signing to undeclared variables. – gcampbell Jul 4 at 15:27

I was wondering if there's a need to split at all, because characters are accessible directly using String#charAt.

This implementation (which is almost the same as yours - only with the split) should be among the fastest.

function reverse(str) {
  var result = [];
  for (var i = str.length - 1; i >= 0; i--) {
    result.push(str.charAt(i));
  }
  return result.join("");
}

console.log(reverse("abcde"));

According to some benchmark, String concatenation is better optimized than Array.join, it also makes the code cleaner:

function reverse(str) {
  var result = "";
  for (var i = str.length - 1; i >= 0; i--) {
    result += str.charAt(i);
  }
  return result;
}

console.log(reverse("abcde"));

As a side-note, you can get creative by using Array.prototype.reduce and allow JavaScript duck-type the String as an Array.

function reverse(str) {
  return Array.prototype.reduce.call(str, function(result, c) {
    return c + result;
  }, "");
}

console.log(reverse("Hello world!"));

And going further you can make an ES6 one-liner:

let reverse = str => Array.prototype.reduce.call(str, (result, c) => c + result, "");

console.log(reverse("Hello world!"));

JSFiddle forked from @Andreas: https://jsfiddle.net/kazenorin/6shbv6hs/2/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.