Created
May 23, 2019 06:11
-
-
Save tjeastmond/86f463f74b4d0f7ef8b97f3ffd112b12 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
// reverse character array | |
const reverse = arr => { | |
if (arr.length === 1) return arr; | |
let left = 0; | |
let right = arr.length - 1; | |
while (left < right) { | |
[arr[left], arr[right]] = [arr[right], arr[left]]; | |
left++; | |
right--; | |
} | |
return arr; | |
}; | |
const reverseString = str => reverse(str.split("")).join(""); | |
const array = ["a", "b", "c", "d", "e", "f", "g"]; | |
const string = "This is a string"; | |
reverse(array); | |
console.log("array: ", array); | |
console.log("reverseString(string): ", reverseString(string)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment