2
2
3
3
// Part One: Reverse Characters
4
4
5
- // 1. Define the function as reverseCharacters. Give it one parameter, which will be the string to reverse.
6
- // 2. Within the function, split the string into an array, then reverse the array.
7
- // 3. Use join to create the reversed string and return that string from the function.
8
- // 4. Below the function, define and initialize a variable to hold a string.
9
- // 5. Use console.log(reverseCharacters(myVariableName)); to call the function and verify that it correctly reverses the characters in the string.
10
- // 6. Optional: Use method chaining to reduce the lines of code within the function.
5
+
6
+ function reverseCharacters ( stringToReverse ) {
7
+ if ( typeof stringToReverse === 'string' ) { }
8
+ if ( typeof stringToReverse === 'number' ) {
9
+ stringToReverse = String ( stringToReverse ) ;
10
+ return Number ( stringToReverse . split ( '' ) . reverse ( ) . join ( '' ) ) ;
11
+ }
12
+ return stringToReverse . split ( '' ) . reverse ( ) . join ( '' ) ;
13
+
14
+ }
15
+ let testString = "apple" ;
16
+ console . log ( reverseCharacters ( testString ) ) ;
17
+
18
+ let testStringTwo = 12345 ;
19
+ console . log ( reverseCharacters ( testStringTwo ) ) ;
11
20
12
21
// Part Two: Reverse Digits
13
22
@@ -30,6 +39,16 @@ let arrayTest1 = ['apple', 'potato', 'Capitalized Words'];
30
39
let arrayTest2 = [ 123 , 8897 , 42 , 1168 , 8675309 ] ;
31
40
let arrayTest3 = [ 'hello' , 'world' , 123 , 'orange' ] ;
32
41
42
+ function reverseEverything ( reversingArr ) {
43
+ let startingArr = [ ] ;
44
+ for ( let index = 0 ; index < reversingArr . length ; index ++ ) {
45
+ startingArr . push ( reverseCharacters ( reversingArr [ index ] ) )
46
+ }
47
+ return startingArr . reverse ( ) ;
48
+ }
49
+
50
+ console . log ( reverseEverything ( arrayTest1 ) ) ;
51
+
33
52
// Bonus Missions
34
53
35
54
// 1. Have a clear, descriptive name like funPhrase.
0 commit comments