exercise3 complete
This commit is contained in:
parent
7499ee5d6f
commit
a4f5ae477b
|
@ -1,6 +1,26 @@
|
||||||
const reverseString = function() {
|
//ALGO 3 - COMPLETE
|
||||||
|
|
||||||
|
/*
|
||||||
|
Pseudocode:
|
||||||
|
- If a string is passed as an argument into the function, we need to return the reversed version of the string.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const reverseString = function(string) {
|
||||||
|
// Store the length of the string inside the loopNum variable. This variable will be so we now how many loop iterations to perform.
|
||||||
|
let loopNum = string.length;
|
||||||
|
// Store an empty string inside reversedString. We will add a letter to the reversedString for each iteation.
|
||||||
|
let reversedString = ''
|
||||||
|
// Create a for loop. Start at index of 1, and as long as i is below length of string. we will continue the loop.
|
||||||
|
// For each iteration of the loop, we add a single character to the string. This character will be i amout of positions from the end of the string. As I grows, we move towards the front of the string.
|
||||||
|
for (i = 1; i <= loopNum; i++) {
|
||||||
|
reversedString += string.charAt(string.length - i);
|
||||||
|
}
|
||||||
|
return reversedString
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let test = reverseString('hello there')
|
||||||
|
console.log(test)
|
||||||
|
|
||||||
// Do not edit below this line
|
// Do not edit below this line
|
||||||
module.exports = reverseString;
|
module.exports = reverseString;
|
||||||
|
|
|
@ -5,14 +5,14 @@ describe('reverseString', () => {
|
||||||
expect(reverseString('hello')).toEqual('olleh');
|
expect(reverseString('hello')).toEqual('olleh');
|
||||||
});
|
});
|
||||||
|
|
||||||
test.skip('reverses multiple words', () => {
|
test('reverses multiple words', () => {
|
||||||
expect(reverseString('hello there')).toEqual('ereht olleh')
|
expect(reverseString('hello there')).toEqual('ereht olleh')
|
||||||
})
|
})
|
||||||
|
|
||||||
test.skip('works with numbers and punctuation', () => {
|
test('works with numbers and punctuation', () => {
|
||||||
expect(reverseString('123! abc!')).toEqual('!cba !321')
|
expect(reverseString('123! abc!')).toEqual('!cba !321')
|
||||||
})
|
})
|
||||||
test.skip('works with blank strings', () => {
|
test('works with blank strings', () => {
|
||||||
expect(reverseString('')).toEqual('')
|
expect(reverseString('')).toEqual('')
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue