-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
45 lines (40 loc) · 1.04 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// --- Directions
// Given a string, return a new string with the reversed order of characters
// --- Examples
// reverseString('apple') === 'alppa'
// reverseString('hello') === 'ollel'
// reverseString('Greetings!') === '!sgniteerG'
/**
* solution3
* step: use reduce
*/
function reverseString(str) {
return str.split('').reduce((reversed, character) => character + reversed, '');
}
module.exports = reverseString;
/**
* solution1
* step:
* 1. turn 'str' into an array
* 2. call 'reverse' method on the array
* 3. join the array back into a string
* 4. return the result
*/
// function reverseString(str) {
// return str.split('').reverse().join('');
// }
/**
* solution2
* step:
* 1. create an empty string called 'reversed'
* 2. for each character in the provided string
* take the character and add it to the start of 'reversed'
* 3. return the variable 'reversed'
*/
// function reverseString(str) {
// let reversed = '';
// for (let character of str) {
// reversed = character + reversed;
// }
// return reversed;
// }