diff --git a/EASY/script.js b/EASY/script.js index 408f822..1ceaae7 100644 --- a/EASY/script.js +++ b/EASY/script.js @@ -195,4 +195,33 @@ const lengthOfLastWord = function (s) { return s.trim().split(" ").pop().length }; -console.log(lengthOfLastWord("luffy is still joyboy")); \ No newline at end of file +console.log(lengthOfLastWord("luffy is still joyboy")); + + + +// const reverseString = function (s) { +// return s.slice().reverse().join() +// }; + +// console.log(reverseString(["h" ,"e" , "l" , "l" , "o"])); + +const reverseString = function (s) { + let left = 0; + let right = s.length - 1; + + while (left < right) { + // Swap the characters at the left and right pointers + [s[left], s[right]] = [s[right], s[left]]; + left++; + right--; + } +}; + +// Test cases +let s1 = ["h", "e", "l", "l", "o"]; +reverseString(s1); +console.log(s1); // Output: ["o", "l", "l", "e", "h"] + +let s2 = ["H", "a", "n", "n", "a", "h"]; +reverseString(s2); +console.log(s2); // Output: ["h", "a", "n", "n", "a", "H"]