diff --git a/03_reverseString/reverseString.js b/03_reverseString/reverseString.js index f6790f0..1cf519d 100644 --- a/03_reverseString/reverseString.js +++ b/03_reverseString/reverseString.js @@ -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 module.exports = reverseString; diff --git a/03_reverseString/reverseString.spec.js b/03_reverseString/reverseString.spec.js index 8adb887..b51c50e 100644 --- a/03_reverseString/reverseString.spec.js +++ b/03_reverseString/reverseString.spec.js @@ -5,14 +5,14 @@ describe('reverseString', () => { expect(reverseString('hello')).toEqual('olleh'); }); - test.skip('reverses multiple words', () => { + test('reverses multiple words', () => { 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') }) - test.skip('works with blank strings', () => { + test('works with blank strings', () => { expect(reverseString('')).toEqual('') }) });