Last active
September 2, 2018 19:52
-
-
Save bennett39/07b6627a2bf3bf5b18241bef3fbd6710 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
<label for="strQuery">Enter a string:</label> | |
<input type="text" id="strQuery"> | |
<button type="submit" id="submit">Submit</button> | |
<p id="result"></p> |
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
/* A web app to reverse a string, using Javascript and JQuery. | |
Recursive solution removes first letter until only one remains, then returns letters in backwards order. | |
Original solution in comments at end using for loop to reverse order. */ | |
function reverseString(str) { | |
if (str == "") | |
return ""; | |
else | |
return reverseString(str.substr(1)) + str.charAt(0); | |
} | |
$("#submit").click(function() { | |
var stringQuery = $("#strQuery").val(); | |
$("#result").html(reverseString(stringQuery)); | |
}); | |
//First Solution (non-recursive): | |
// var result = ""; | |
// for (var i = str.length - 1; i >= 0 ; i--) { | |
// result += str[i]; | |
// } |
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
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment