-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay20.js
More file actions
32 lines (24 loc) · 790 Bytes
/
Day20.js
File metadata and controls
32 lines (24 loc) · 790 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// *
//* Coding Challenge
//*-
//?20. Write a function to reverse a string without using any built-in methods or libraries. The function should take a string as input and return the reversed string.
// Example usage:
// 1st method
// const reverseString = (str) =>{
// let revstr = "";
// for(let i=str.length; i>=0; i--){
// revstr = revstr + str.charAt(i);
// }
// return revstr;
// }
// console.log(reverseString("hello")); // Output: olleH
// 2nd method
const reverseString = (str) => {
// Convert the string to an array of characters
let charArray = str.split("");
// Reverse the array
charArray.reverse();
// Join the array back into a string
return charArray.join("");
}
console.log(reverseString("hello")); // Output: olleh