-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreverseString.js
35 lines (35 loc) · 934 Bytes
/
reverseString.js
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
33
34
35
"use strict";
/**
*
* @param {string} str
* @return {string}
*/
// function reverseString(str: string): string {
// return str
// .split("")
// .reverse()
// .join("");
// }
// function reverseString(str: string): string {
// let reverse: string = "";
// for (const char of str) {
// reverse = char + reverse;
// }
// return reverse;
// }
// function reverseString(str: string): string {
// return str.split("").reduce((reversed: string, char: string): string => {
// return (reversed = char + reversed);
// }, "");
// }
function reverseString(str) {
var newArr = [];
for (var k = 0; k < str.length; k++) {
newArr.push(str[str.length - k - 1]);
}
return newArr.join('');
}
console.log(reverseString("apple") === "elppa");
console.log(reverseString("hello") === "olleh");
console.log(reverseString("Greetings!") === "!sgniteerG");
console.log(reverseString("typescript"));