Last active
April 30, 2018 21:26
-
-
Save MorenoMdz/c7e98461cbddf78dedce0c6d87240eb9 to your computer and use it in GitHub Desktop.
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
function FirstReverse(str) { | |
// the new string, reversed, that will be returned. | |
let newString = ''; | |
// at each iteration, i is the size of the string less one (right index), | |
// while str.length - 1 is above zero, | |
// subtract 1 | |
for (var i = str.length - 1; i >= 0; i--) { | |
// at each index iteration, the newString will receive a character that is the | |
// character at the current index (str.length -1), | |
// then the index will shorten by one at every iteration. | |
newString = newString + str.charAt(i); | |
} | |
return newString; | |
} | |
// OR | |
function FirstReverse2(str) { | |
// first we split the string which creates an array of characters, e.g. ['c','a','t'] | |
// then we call the reverse function on this array | |
// and finally we turn the reversed array back into a string using the join function | |
return str | |
.split('') | |
.reverse() | |
.join(''); | |
} | |
FirstReverse2(readline()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment