|
| 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 | +// Part Two: Reverse Digits |
| 13 | + |
| 14 | +// 1. Add an if statement to reverseCharacters to check the typeof the parameter. |
| 15 | +// 2. If typeof is ‘string’, return the reversed string as before. |
| 16 | +// 3. If typeof is ’number’, convert the parameter to a string, reverse the characters, then convert it back into a number. |
| 17 | +// 4. Return the reversed number. |
| 18 | +// 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. |
| 19 | + |
| 20 | +// Part Three: Complete Reversal |
| 21 | + |
| 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. |
| 27 | +// 6. Be sure to print the results from each test case in order to verify your code. |
| 28 | + |
| 29 | +let arrayTest1 = ['apple', 'potato', 'Capitalized Words']; |
| 30 | +let arrayTest2 = [123, 8897, 42, 1168, 8675309]; |
| 31 | +let arrayTest3 = ['hello', 'world', 123, 'orange']; |
| 32 | + |
| 33 | +// Bonus Missions |
| 34 | + |
| 35 | +// 1. Have a clear, descriptive name like funPhrase. |
| 36 | +// 2. Retrieve only the last character from strings with lengths of 3 or less. |
| 37 | +// 3. Retrieve only the first 3 characters from strings with lengths larger than 3. |
| 38 | +// 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. |
| 39 | + |
| 40 | +// Test Function |
| 41 | + |
| 42 | +// 1. Outside of the function, define the variable str and initialize it with a string (e.g. 'Functions rock!'). |
| 43 | +// 2. Call your function and print the returned phrase. |
| 44 | + |
| 45 | +// Area of rectangle equal to length x width |
| 46 | + |
| 47 | +// 1. Define a function with the required parameters to calculate the area of a rectangle. |
| 48 | +// 2. The function should return the area, NOT print it. |
| 49 | +// 3. Call your area function by passing in two arguments - the length and width. |
| 50 | +// 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 | +// 5. Use a template literal to print, “The area is ____ cm^2.” |
0 commit comments