Skip to content

Commit 1bf92f9

Browse files
committed
Reverse a String
1 parent 8aeb048 commit 1bf92f9

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Diff for: 3_Reverse-a-String/index.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Reverse a String
2+
// Write a function that takes in a string and reverses its value
3+
// ('code') => 'edoc'
4+
5+
const reverseString = str => {
6+
return str
7+
.split('')
8+
.reverse()
9+
.join('')
10+
}
11+
12+
console.log(reverseString('code'))
13+
console.log(reverseString('reverse'))
14+
console.log(reverseString('hey buddy'))
15+
16+
//
17+
//
18+
// or using a for-loop
19+
const reverseStringLoop = str => {
20+
let revstr = ''
21+
for (let i = str.length - 1; i >= 0; i--) {
22+
// revstr += str.charAt(i)
23+
revstr += str[i]
24+
}
25+
return revstr
26+
}
27+
28+
console.log(reverseStringLoop('code'))
29+
console.log(reverseStringLoop('reverse'))
30+
console.log(reverseStringLoop('hey buddy'))

0 commit comments

Comments
 (0)