1
- //We want to COMPLETELY reverse an array by flipping the order of the entries AND flipping the order of characters in each element.
1
+ //We want to COMPLETELY reverse an stray by flipping the order of the entries AND flipping the order of characters in each element.
2
+
3
+ function reverseCharacters ( str ) {
4
+ if ( typeof str === "number" ) {
5
+ return Number ( String ( str ) . split ( "" ) . reverse ( ) . join ( "" ) )
6
+ }
7
+ return str . split ( "" ) . reverse ( ) . join ( "" )
8
+
9
+
10
+ }
11
+
12
+ let myVariableName = 1234567
13
+
14
+ console . log ( reverseCharacters ( myVariableName ) )
15
+
2
16
3
17
// Part One: Reverse Characters
4
18
5
19
// 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.
20
+ // 2. Within the function, split the string into an stray, then reverse the stray.
21
+
7
22
// 3. Use join to create the reversed string and return that string from the function.
23
+
24
+
8
25
// 4. Below the function, define and initialize a variable to hold a string.
26
+
27
+
9
28
// 5. Use console.log(reverseCharacters(myVariableName)); to call the function and verify that it correctly reverses the characters in the string.
29
+
30
+
10
31
// 6. Optional: Use method chaining to reduce the lines of code within the function.
11
32
12
33
// Part Two: Reverse Digits
19
40
20
41
// Part Three: Complete Reversal
21
42
22
- // 1. Define and initialize an empty array .
23
- // 2. Loop through the old array .
24
- // 3. For each element in the old array , call reverseCharacters to flip the characters or digits.
25
- // 4. Add the reversed string (or number) to the array defined in part ‘a’.
26
- // 5. Return the final, reversed array .
43
+ // 1. Define and initialize an empty stray .
44
+ // 2. Loop through the old stray .
45
+ // 3. For each element in the old stray , call reverseCharacters to flip the characters or digits.
46
+ // 4. Add the reversed string (or number) to the stray defined in part ‘a’.
47
+ // 5. Return the final, reversed stray .
27
48
// 6. Be sure to print the results from each test case in order to verify your code.
28
49
29
- let arrayTest1 = [ 'apple' , 'potato' , 'Capitalized Words' ] ;
30
- let arrayTest2 = [ 123 , 8897 , 42 , 1168 , 8675309 ] ;
31
- let arrayTest3 = [ 'hello' , 'world' , 123 , 'orange' ] ;
50
+ let strayTest1 = [ 'apple' , 'potato' , 'Capitalized Words' ] ;
51
+ let strayTest2 = [ 123 , 8897 , 42 , 1168 , 8675309 ] ;
52
+ let strayTest3 = [ 'hello' , 'world' , 123 , 'orange' ] ;
32
53
54
+ function completeReversal ( oldArr ) {
55
+ let arr = [ ]
56
+ for ( i = 0 ; i < oldArr . length ; i ++ ) {
57
+ arr . unshift ( reverseCharacters ( oldArr [ i ] ) )
58
+ }
59
+ return arr
60
+ }
61
+ console . log ( completeReversal ( strayTest1 ) )
33
62
// Bonus Missions
34
63
35
64
// 1. Have a clear, descriptive name like funPhrase.
@@ -49,3 +78,6 @@ let arrayTest3 = ['hello', 'world', 123, 'orange'];
49
78
// 3. Call your area function by passing in two arguments - the length and width.
50
79
// 4. If only one argument is passed to the function, then the shape is a square. Modify your code to deal with this case.
51
80
// 5. Use a template literal to print, “The area is ____ cm^2.”
81
+
82
+ function areaOfRectangle ( )
83
+
0 commit comments