Created
March 27, 2015 17:19
-
-
Save troyleach/4894809737e98fc2e212 to your computer and use it in GitHub Desktop.
reverse a string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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