|
1 |
| -//We want to COMPLETELY reverse an array by flipping the order of the entries AND flipping the order of characters in each element. |
2 |
| - |
3 |
| -// Part One: Reverse Characters |
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. |
11 |
| - |
12 |
| -function reverseCharacters(str) { |
13 |
| - |
14 |
| - |
15 |
| - |
16 |
| - if ( typeof(str) ===typeof("f")){ |
17 |
| - |
18 |
| - let lettersArray = str.split(''); |
19 |
| - |
20 |
| - let reversedLettersArray = lettersArray.reverse(); |
21 |
| - |
22 |
| - return reversedLettersArray.join(''); |
23 |
| - |
24 |
| -} |
25 |
| - if (typeof(str) === typeof(11)){ |
26 |
| - let lettersArray = str.toString(); |
27 |
| - |
28 |
| - let splitThis =lettersArray.split(''); |
29 |
| - |
30 |
| - let reversedLettersArray = splitThis.reverse(); |
31 |
| - |
32 |
| - reversedLettersArray=reversedLettersArray.join(''); |
33 |
| - |
34 |
| - reversedLettersArray=Number(reversedLettersArray); |
35 |
| - |
36 |
| - return reversedLettersArray; |
37 |
| - |
38 |
| - |
39 |
| -} |
40 |
| - |
41 |
| - |
42 |
| - |
43 |
| - |
44 |
| - } |
45 |
| - |
46 |
| -console.log(reverseCharacters("taco")); |
47 |
| - |
48 |
| - |
49 |
| - |
50 |
| - |
51 |
| -// Part Two: Reverse Digits |
52 |
| - |
53 |
| -// 1. Add an if statement to reverseCharacters to check the typeof the parameter. |
54 |
| -// 2. If typeof is ‘string’, return the reversed string as before. |
55 |
| -// 3. If typeof is ’number’, convert the parameter to a string, reverse the characters, then convert it back into a number. |
56 |
| -// 4. Return the reversed number. |
57 |
| -// 5. Be sure to print the result returned by the function to verify that your code works for both strings and numbers. Do this before moving on to the next exercise. |
58 |
| - |
59 |
| -// Part Three: Complete Reversal |
60 |
| - |
61 |
| -// 1. Define and initialize an empty array. |
62 |
| -// 2. Loop through the old array. |
63 |
| -// 3. For each element in the old array, call reverseCharacters to flip the characters or digits. |
64 |
| -// 4. Add the reversed string (or number) to the array defined in part ‘a’. |
65 |
| -// 5. Return the final, reversed array. |
66 |
| -// 6. Be sure to print the results from each test case in order to verify your code. |
67 |
| -let empty = []; |
68 |
| -let arrayTest1 = ['apple', 'potato', 'Capitalized Words']; |
69 |
| -let arrayTest2 = [123, 8897, 42, 1168, 8675309]; |
70 |
| -let arrayTest3 = ['hello', 'world', 123, 'orange']; |
71 |
| - |
72 |
| -function completeReversal(array){ |
73 |
| - |
74 |
| -let reversedArray=[]; |
75 |
| -for (let i=0; i<array.length;i++){ |
76 |
| - |
77 |
| -let reversedChar = reverseCharacters(array[i]); |
78 |
| -reversedArray.push(reversedChar) |
79 |
| -// |
80 |
| - |
81 |
| - |
82 |
| -} |
83 |
| -return reversedArray; |
84 |
| -} |
85 |
| -// console.log(completeReversal(arrayTest1)); |
86 |
| -// console.log(completeReversal(arrayTest2)); |
87 |
| -// console.log(completeReversal(arrayTest3)); |
88 |
| -// Bonus Missions |
89 |
| - |
90 |
| -// 1. Have a clear, descriptive name like funPhrase. |
91 |
| -// 2. Retrieve only the last character from strings with lengths of 3 or less. |
92 |
| -// 3. Retrieve only the first 3 characters from strings with lengths larger than 3. |
93 |
| -// 4. Use a template literal to return the phrase We put the '___' in '___'. Fill the first blank with the modified string, and fill the second blank with the original string. |
94 |
| - |
95 |
| - |
96 |
| - |
97 |
| - |
98 |
| - |
99 |
| - |
100 |
| - |
101 |
| - |
102 |
| -// Test Function |
103 |
| - |
104 |
| -// 1. Outside of the function, define the variable str and initialize it with a string (e.g. 'Functions rock!'). |
105 |
| -// 2. Call your function and print the returned phrase. |
106 |
| - |
107 |
| -// Area of rectangle equal to length x width |
108 |
| - |
109 |
| -// 1. Define a function with the required parameters to calculate the area of a rectangle. |
110 |
| -// 2. The function should return the area, NOT print it. |
111 |
| -// 3. Call your area function by passing in two arguments - the length and width. |
112 |
| -// 4. If only one argument is passed to the function, then the shape is a square. Modify your code to deal with this case. |
113 |
| -// 5. Use a template literal to print, “The area is ____ cm^2.” |
| 1 | +//We want to COMPLETELY reverse an array by flipping the order of the entries AND flipping the order of characters in each element. |
| 2 | + |
| 3 | +// Part One: Reverse Characters |
| 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. |
| 11 | + |
| 12 | +function reverseCharacters(str) { |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | + if ( typeof(str) ===typeof("f")){ |
| 17 | + |
| 18 | + let lettersArray = str.split(''); |
| 19 | + |
| 20 | + let reversedLettersArray = lettersArray.reverse(); |
| 21 | + |
| 22 | + return reversedLettersArray.join(''); |
| 23 | + |
| 24 | +} |
| 25 | + if (typeof(str) === typeof(11)){ |
| 26 | + let lettersArray = str.toString(); |
| 27 | + |
| 28 | + let splitThis =lettersArray.split(''); |
| 29 | + |
| 30 | + let reversedLettersArray = splitThis.reverse(); |
| 31 | + |
| 32 | + reversedLettersArray=reversedLettersArray.join(''); |
| 33 | + |
| 34 | + reversedLettersArray=Number(reversedLettersArray); |
| 35 | + |
| 36 | + return reversedLettersArray; |
| 37 | + |
| 38 | + |
| 39 | +} |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | +console.log(reverseCharacters("taco")); |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +// Part Two: Reverse Digits |
| 52 | + |
| 53 | +// 1. Add an if statement to reverseCharacters to check the typeof the parameter. |
| 54 | +// 2. If typeof is ‘string’, return the reversed string as before. |
| 55 | +// 3. If typeof is ’number’, convert the parameter to a string, reverse the characters, then convert it back into a number. |
| 56 | +// 4. Return the reversed number. |
| 57 | +// 5. Be sure to print the result returned by the function to verify that your code works for both strings and numbers. Do this before moving on to the next exercise. |
| 58 | + |
| 59 | +// Part Three: Complete Reversal |
| 60 | + |
| 61 | +// 1. Define and initialize an empty array. |
| 62 | +// 2. Loop through the old array. |
| 63 | +// 3. For each element in the old array, call reverseCharacters to flip the characters or digits. |
| 64 | +// 4. Add the reversed string (or number) to the array defined in part ‘a’. |
| 65 | +// 5. Return the final, reversed array. |
| 66 | +// 6. Be sure to print the results from each test case in order to verify your code. |
| 67 | +let empty = []; |
| 68 | +let arrayTest1 = ['apple', 'potato', 'Capitalized Words']; |
| 69 | +let arrayTest2 = [123, 8897, 42, 1168, 8675309]; |
| 70 | +let arrayTest3 = ['hello', 'world', 123, 'orange']; |
| 71 | + |
| 72 | +function completeReversal(array){ |
| 73 | + |
| 74 | +let reversedArray=[]; |
| 75 | +for (let i=0; i<array.length;i++){ |
| 76 | + |
| 77 | +let reversedChar = reverseCharacters(array[i]); |
| 78 | +reversedArray.push(reversedChar) |
| 79 | +// |
| 80 | + |
| 81 | + |
| 82 | +} |
| 83 | +return reversedArray; |
| 84 | +} |
| 85 | +// console.log(completeReversal(arrayTest1)); |
| 86 | +// console.log(completeReversal(arrayTest2)); |
| 87 | +// console.log(completeReversal(arrayTest3)); |
| 88 | +// Bonus Missions |
| 89 | + |
| 90 | +// 1. Have a clear, descriptive name like funPhrase. |
| 91 | +// 2. Retrieve only the last character from strings with lengths of 3 or less. |
| 92 | +// 3. Retrieve only the first 3 characters from strings with lengths larger than 3. |
| 93 | +// 4. Use a template literal to return the phrase We put the '___' in '___'. Fill the first blank with the modified string, and fill the second blank with the original string. |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | +// Test Function |
| 103 | + |
| 104 | +// 1. Outside of the function, define the variable str and initialize it with a string (e.g. 'Functions rock!'). |
| 105 | +// 2. Call your function and print the returned phrase. |
| 106 | + |
| 107 | +// Area of rectangle equal to length x width |
| 108 | + |
| 109 | +// 1. Define a function with the required parameters to calculate the area of a rectangle. |
| 110 | +// 2. The function should return the area, NOT print it. |
| 111 | +// 3. Call your area function by passing in two arguments - the length and width. |
| 112 | +// 4. If only one argument is passed to the function, then the shape is a square. Modify your code to deal with this case. |
| 113 | +// 5. Use a template literal to print, “The area is ____ cm^2.” |
0 commit comments