Skip to content

Commit

Permalink
reverse string completed
Browse files Browse the repository at this point in the history
  • Loading branch information
MKAIF5 committed Aug 16, 2024
1 parent 4cbd73f commit f64bfcc
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion EASY/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,33 @@ const lengthOfLastWord = function (s) {
return s.trim().split(" ").pop().length
};

console.log(lengthOfLastWord("luffy is still joyboy"));
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"]

0 comments on commit f64bfcc

Please sign in to comment.