Last active
April 21, 2017 05:40
-
-
Save schan90/8de46a23188bf3f794be7fc72c20692a to your computer and use it in GitHub Desktop.
Week1 firstReverse
This file contains hidden or 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
| var userInput = prompt("Enter a String:"); | |
| function firstReverse(str) { | |
| var result = ''; | |
| for(var i=0; i<str.length; i++){ | |
| result+=str.charAt(str.length-1-i); | |
| } | |
| return result; | |
| } | |
| console.log(firstReverse(userInput)); |
This file contains hidden or 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 reverse(s) { | |
| if (s.length < 2) | |
| return s; | |
| var halfIndex = Math.ceil(s.length / 2); | |
| return reverse(s.substr(halfIndex)) + | |
| reverse(s.substr(0, halfIndex)); | |
| } |
This file contains hidden or 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 reverse(s) { | |
| var o = ''; | |
| for (var i = s.length - 1; i >= 0; i--) | |
| o += s[i]; | |
| return o; | |
| } |
This file contains hidden or 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 reverse(s) { | |
| var o = []; | |
| for (var i = s.length - 1, j = 0; i >= 0; i--, j++) | |
| o[j] = s[i]; | |
| return o.join(''); | |
| } |
This file contains hidden or 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 reverse(s) { | |
| var o = []; | |
| for (var i = 0, len = s.length; i <= len; i++) | |
| o.push(s.charAt(len - i)); | |
| return o.join(''); | |
| } |
This file contains hidden or 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 reverse(s) { | |
| return s.split('').reverse().join(''); | |
| } |
This file contains hidden or 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 reverse(s) { | |
| var i = s.length, | |
| o = ''; | |
| while (i > 0) { | |
| o += s.substring(i - 1, i); | |
| i--; | |
| } | |
| return o; | |
| } |
This file contains hidden or 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 reverse(s) { | |
| return (s === '') ? '' : reverse(s.substr(1)) + s.charAt(0); | |
| } |
This file contains hidden or 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 reverse(s) { | |
| function rev(s, len, o) { | |
| return (len === 0) ? o : rev(s, --len, (o += s[len])); | |
| }; | |
| return rev(s, s.length, ''); | |
| } |
This file contains hidden or 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 reverse(s) { | |
| s = s.split(''); | |
| var len = s.length, | |
| halfIndex = Math.floor(len / 2) - 1, | |
| tmp; | |
| for (var i = 0; i <= halfIndex; i++) { | |
| tmp = s[len - i - 1]; | |
| s[len - i - 1] = s[i]; | |
| s[i] = tmp; | |
| } | |
| return s.join(''); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment