-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
48 lines (40 loc) · 1.07 KB
/
index.js
File metadata and controls
48 lines (40 loc) · 1.07 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
// reverse with spread operator
let reverseString = (str) => {
// reverse string with reverse and join method
// first we should convert it to an array
newStr = [...str].reverse().join('')
console.log(newStr)
}
reverseString("hello");
// reverse with split
let reverseStringSplit = (str) => {
// reverse string with reverse and join method
// first we should convert it to an array
newStr = str.split('').reverse().join('')
console.log(newStr)
}
reverseStringSplit("bye");
// reverse string with for loop
let reverseStringForLoop = (str) => {
let newStr = '';
for( let i = str.length - 1; i >= 0; i --) {
newStr += str[i]
}
console.log(newStr)
}
reverseStringForLoop('!oG')
// reverse string with for... of loop
let reverseStringForOfLoop = (str) => {
let newStr = '';
for( let letter of str) {
newStr = letter + newStr
}
console.log(newStr)
}
reverseStringForOfLoop('!emosewA')
// reverse string
let reverseStringWReduce = (str) => {
let newString = [...str].reduce((acc, word) => word + acc)
console.log(newString)
}
reverseStringWReduce('!dooG')