10.11. Studio: Functions¶
The reverse
method flips the order of the elements within an array.
However, reverse
does not affect the digits or characters within those
elements.
Example
1 2 3 4 | let arr = ['hello', 'world', 123, 'orange'];
arr.reverse()
console.log(arr);
|
Console Output
['orange', 123, 'world', 'hello']
What if we wanted the reversed array to be
['egnaro', 321, 'dlrow', 'olleh']
?
Let's have some fun by creating a process that reverses BOTH the order of the entries in an array AND the order of characters within the individual elements.
Remember that a function should perform only one task. To follow this best practice, we will solve the array reversal by defining two functions - one that reverses the characters in a string (or the digits in a number) and one that flips the order of entries in the array.
10.11.1. Reverse Characters¶
In the composing functions section, we examined a function that reverses the characters in a string using the
split
andjoin
methods. Let's rebuild that function now.Define the function as
reverseCharacters
. Give it one parameter, which will be the string to reverse.Within the function,
split
the string into an array, then reverse the array.Use
join
to create the reversed string and return that string from the function.Below the function, define and initialize a variable to hold a string.
Use
console.log(reverseCharacters(myVariableName));
to call the function and verify that it correctly reverses the characters in the string.Optional: Use method chaining to reduce the lines of code within the function.
Code exercises 1 - 3 at repl.it
Tip
Use these sample strings for testing:
'apple'
'LC101'
'Capitalized Letters'
'I love the smell of code in the morning.'
10.11.2. Reverse Digits¶
The
reverseCharacters
function works great on strings, but what if the argument passed to the function is a number? Usingconsole.log(reverseCharacters(1234));
results in an error, sincesplit
only works on strings (TRY IT). When passed a number, we want the function to return a number with all the digits reversed (e.g. 1234 converts to 4321 and NOT the string"4321"
).Add an
if
statement toreverseCharacters
to check thetypeof
the parameter.If
typeof
is 'string', return the reversed string as before.If
typeof
is 'number', convert the parameter to a string, reverse the characters, then convert it back into a number.Return the reversed number.
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.
Tip
Use these samples for testing:
1234
'LC101'
8675309
'radar'
10.11.3. Complete Reversal¶
Now we are ready to finish our complete reversal process. Create a new function with one parameter, which is the array we want to change. The function should:
Define and initialize an empty array.
Loop through the old array.
For each element in the old array, call
reverseCharacters
to flip the characters or digits.Add the reversed string (or number) to the array defined in part 'a'.
Return the final, reversed array.
Be sure to print the results from each test case in order to verify your code.
Tip
Use this sample data for testing.
Input |
Output |
---|---|
|
|
|
|
|
|
10.11.4. Bonus Missions¶
Define a function with one parameter, which will be a string. The function must do the following:
Have a clear, descriptive name like
funPhrase
.Retrieve only the last character from strings with lengths of 3 or less.
Retrieve only the first 3 characters from strings with lengths larger than 3.
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.
Now test your function:
Outside of the function, define the variable
str
and initialize it with a string (e.g.'Functions rock!'
).Call your function and print the returned phrase.
The area of a rectangle is equal to its length x width.
Define a function with the required parameters to calculate the area of a rectangle.
The function should return the area, NOT print it.
Call your area function by passing in two arguments - the length and width.
If only one argument is passed to the function, then the shape is a square. Modify your code to deal with this case.
Use a template literal to print, "The area is ____ cm^2."
Tip
Use these test cases.
length = 2, width = 4 (area = 8)
length = 14, width = 7 (area = 98)
length = 20 (area = 400)