Skip to main content

Section 5.23 Functions and Strings Mixed-Up Code Questions

Checkpoint 5.23.1.

Create a function joinStrings(str1, str2) that takes two different strings as parameters, str1 and str2, and returns them as one string with str1 before str2. For example, joinStrings('he','llo') returns hello.

Checkpoint 5.23.2.

Create a function rect_per(w, h) that takes the width, w, and the height, h, as parameters. Calculate the perimeter, which is the width plus the height times two, and return a string in the form - Perimeter of rectangle with width of (w) and height of (h) is (perimeter). For example, rect_per(5,10) returns Perimeter of rectangle with width of 5 and height of 10 is 30.

Checkpoint 5.23.3.

Create a function cube(n) that takes a number, n, and cubes it and returns a string in the form Cube of (n) is (n cubed). For example, cube(4) returns Cube of 4 is 64.

Checkpoint 5.23.4.

Create a function atlas(c_name) that takes a country name, c_name, as a parameter and returns a string in the form of The last letter of (c_name) is (last). For example, atlas('Germany') returns The last letter of Germany is y.

Checkpoint 5.23.5.

Create a function dateYear(date) that takes a string, date, in the format MM/DD/YYYY and returns the year. For example, dateYear(11/07/2000) should return 2000.

Checkpoint 5.23.6.

Create a function capitalize(str), that takes a string, str, and returns the string with the first letter capitalized. For example, capitalize('america') would return America.

Checkpoint 5.23.7.

Create a function mod_password(password), that takes a string, password, and returns a new string that replaces all 's' in the string with '$'. For example, mod_password('suspense') returns $u$pen$e.

Checkpoint 5.23.8.

Create a function first_last(str), that takes a string, str, and returns a new string with the first two characters of the word followed by the last two characters. Assume str is four characters or more. For example, first_last('wander') returns waer.