Skip to content

Commit eb3f821

Browse files
committed
feat: add reverse every other word solution
1 parent 1b8b556 commit eb3f821

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const reverse = (str) => {
2+
str = str.trim().split(' ');
3+
for (let i = 1; i < str.length; i += 2) {
4+
str[i] = str[i].split('').reverse().join('');
5+
}
6+
return str.join(' ');
7+
};
8+
9+
module.exports = reverse;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const reverse = require('./solution');
2+
3+
describe('Reverse every other word in the string', () => {
4+
const testCases = [
5+
{
6+
input: 'Did it work?',
7+
output: 'Did ti work?',
8+
},
9+
{
10+
input: 'I really hope it works this time...',
11+
output: 'I yllaer hope ti works siht time...',
12+
},
13+
{
14+
input: 'Reverse this string, please!',
15+
output: 'Reverse siht string, !esaelp',
16+
},
17+
];
18+
19+
it('should return a string type', () => {
20+
expect(typeof reverse('Did it work?')).toBe('string');
21+
});
22+
23+
it.each(testCases)('should return the correct output', ({ input, output }) => {
24+
expect(reverse(input)).toBe(output);
25+
});
26+
});

0 commit comments

Comments
 (0)