Skip to content

Instantly share code, notes, and snippets.

@tjeastmond
Created May 23, 2019 06:11
Show Gist options
  • Save tjeastmond/86f463f74b4d0f7ef8b97f3ffd112b12 to your computer and use it in GitHub Desktop.
Save tjeastmond/86f463f74b4d0f7ef8b97f3ffd112b12 to your computer and use it in GitHub Desktop.
// 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