Skip to content

Instantly share code, notes, and snippets.

@troyleach
Created March 27, 2015 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save troyleach/4894809737e98fc2e212 to your computer and use it in GitHub Desktop.
Save troyleach/4894809737e98fc2e212 to your computer and use it in GitHub Desktop.
reverse a string
// Sure seems like a lot of code just to reverse a string!
// Define a function reverse() that computes the reversal of a string. For example, reverse("jag testar")
// should return the string "ratset gaj".
var new_str = ""
function reverse(string) {
for (var i = string.length - 1; i >= 0; i--) {
new_str += string[i];
}
return new_str;
}
console.log(reverse('jag testar'));
var string = 'ratset gaj';
console.log(new_str === string);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment