Skip to content

Instantly share code, notes, and snippets.

@NicholasEli
Created February 20, 2020 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NicholasEli/0b6ae0cc1a78fe2e8cc0b453704586bc to your computer and use it in GitHub Desktop.
Save NicholasEli/0b6ae0cc1a78fe2e8cc0b453704586bc to your computer and use it in GitHub Desktop.
Javascript Reverse Array with String Values
/*
Write a function that reverses a string. The input string is given as an array of characters char[].
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
You may assume all the characters consist of printable ascii characters.
*/
const arr = ["h", "e", "l", "l", "o"]
var reverseString = function(s) {
let left = 0
let right = s.length - 1
while(left < right){
let temp = s[left]
s[left++] = s[right]
s[right--] = temp
}
return s
}
reverseString(arr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment