Skip to content

Commit

Permalink
SOLUTION StephenGrider#3
Browse files Browse the repository at this point in the history
- .reduce() helper used.
  • Loading branch information
foxlioncode committed Jan 2, 2020
1 parent 676a7e7 commit aef745c
Showing 1 changed file with 4 additions and 13 deletions.
17 changes: 4 additions & 13 deletions exercises/reversestring/index.js
Expand Up @@ -6,22 +6,13 @@
// reverse('hello') === 'olleh'
// reverse('Greetings!') === '!sgniteerG'

// SOLUTION #2
// SOLUTION #3
function reverse(str) {

// Declare an empty string.
let reversed = '';
// Reduce helper takes all values in array and condenses it down into a single string value.
// <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce>
return str.split('').reduce((reversed, character) => character + reversed, '')

// Avoid classic for loop syntax because it is easy to introduce mistakes with this long form.
// for ... of
// <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of>
// May not work for nth element iteraction.

for (let character of str) {
reversed = character + reversed;
}

return reversed;
}

module.exports = reverse;

0 comments on commit aef745c

Please sign in to comment.