Add files via upload
This commit is contained in:
parent
85ea76b0a5
commit
277d606197
|
@ -0,0 +1,12 @@
|
|||
# Exercise 03 - Reverse a String
|
||||
|
||||
Pretty simple, write a function called `reverseString` that returns its input, reversed!
|
||||
|
||||
```javascript
|
||||
reverseString('hello there') // returns 'ereht olleh'
|
||||
```
|
||||
|
||||
You will notice in this exercise that there are multiple tests, after making the first one pass, enable the others one by one by deleting the `.skip` in front the `test.skip()` function.
|
||||
|
||||
## Hints
|
||||
Strings in JavaScript cannot be reversed directly so you're going to have to split it into something else first.. do the reversal and then join it back together into a string.
|
|
@ -0,0 +1,15 @@
|
|||
const reverseString = function(string) {
|
||||
|
||||
const splitString = string.split("");
|
||||
let reverse = "";
|
||||
|
||||
for (let i = (splitString.length - 1); i >= 0; i--) {
|
||||
const pullString = splitString[i];
|
||||
reverse += pullString;
|
||||
}
|
||||
|
||||
return reverse;
|
||||
};
|
||||
|
||||
// Do not edit below this line
|
||||
module.exports = reverseString;
|
|
@ -0,0 +1,18 @@
|
|||
const reverseString = require('./reverseString')
|
||||
|
||||
describe('reverseString', () => {
|
||||
test('reverses single word', () => {
|
||||
expect(reverseString('hello')).toEqual('olleh');
|
||||
});
|
||||
|
||||
test('reverses multiple words', () => {
|
||||
expect(reverseString('hello there')).toEqual('ereht olleh')
|
||||
})
|
||||
|
||||
test('works with numbers and punctuation', () => {
|
||||
expect(reverseString('123! abc!')).toEqual('!cba !321')
|
||||
})
|
||||
test('works with blank strings', () => {
|
||||
expect(reverseString('')).toEqual('')
|
||||
})
|
||||
});
|
Loading…
Reference in New Issue