Created
February 20, 2020 22:29
-
-
Save NicholasEli/0b6ae0cc1a78fe2e8cc0b453704586bc to your computer and use it in GitHub Desktop.
Javascript Reverse Array with String Values
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
/* | |
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