|
9 | 9 | // 5. Use console.log(reverseCharacters(myVariableName)); to call the function and verify that it correctly reverses the characters in the string.
|
10 | 10 | // 6. Optional: Use method chaining to reduce the lines of code within the function.
|
11 | 11 |
|
| 12 | +function reverseCharacters(str) { |
| 13 | + let tempStr = String(str) |
| 14 | + let spltStr = tempStr.split(""); |
| 15 | + let tempArray = []; |
| 16 | + for (let i = 0; i < tempStr.length; i++){ |
| 17 | + tempArray.push(tempStr[-i + tempStr.length -1]); |
| 18 | + } |
| 19 | + let finalReversed = tempArray.join(""); |
| 20 | + if (typeof str === Number) { |
| 21 | + Number(finalReversed); |
| 22 | + } |
| 23 | + |
| 24 | + return finalReversed; |
| 25 | + } |
| 26 | + |
| 27 | + let testString = 123456789; |
| 28 | + console.log(reverseCharacters(testString)); |
| 29 | + |
12 | 30 | // Part Two: Reverse Digits
|
13 | 31 |
|
14 | 32 | // 1. Add an if statement to reverseCharacters to check the typeof the parameter.
|
|
19 | 37 |
|
20 | 38 | // Part Three: Complete Reversal
|
21 | 39 |
|
| 40 | +function completeReversal(Arr) { |
| 41 | + newArray = []; |
| 42 | + for (let i = 0; i < Arr.length; i++) { |
| 43 | + newArray.push(reverseCharacters(Arr[-i + Arr.length -1])); |
| 44 | + } |
| 45 | + return newArray; |
| 46 | +} |
| 47 | + |
| 48 | + |
22 | 49 | // 1. Define and initialize an empty array.
|
23 | 50 | // 2. Loop through the old array.
|
24 | 51 | // 3. For each element in the old array, call reverseCharacters to flip the characters or digits.
|
|
29 | 56 | let arrayTest1 = ['apple', 'potato', 'Capitalized Words'];
|
30 | 57 | let arrayTest2 = [123, 8897, 42, 1168, 8675309];
|
31 | 58 | let arrayTest3 = ['hello', 'world', 123, 'orange'];
|
32 |
| - |
| 59 | +console.log(completeReversal(arrayTest3)); |
33 | 60 | // Bonus Missions
|
34 | 61 |
|
35 | 62 | // 1. Have a clear, descriptive name like funPhrase.
|
|
0 commit comments