What's Your Junction?
- Use control flow statements within a function to program dynamic behavior based upon varying input arguments.
Listen to Conjunction Junction by Schoolhouse Rock!, performed by Jack Sheldon, Terry Morel, and Mary Sue Berry in 1973.
This lab requires that you type a function's argument as an array containing strings. The syntax for this is to wrap the name of the type to be held in the array with square brackets [
]
. So to type an argument as an array of type String
, it would be written as [String]
. Upcoming lessons will discuss types and typed collections in more detail.
Open swift-conjunction-function.xcworkspace
and navigate to the Sandbox.swift
file.
Write a function named listWithElements(_:conjunction:)
that takes two arguments, an array of strings named elements
and a string named conjunction
, and returns a string. The returned string should contain the strings from the elements
array but organized into a list phrase using the conjunction
argument. This means that:
- If the
elements
argument is empty, the function should return an empty string. - A single string in the
elements
array should result to just that same string, as in "one". - Two strings in the
elements
array should be joined with theconjunction
argument, as in "one and two". - Three or more strings in the
elements
array should be joined with commas (,
), with the conjunction also joining the last element in the list phrase, as in "one, two, or three".
Hint: Consider using aswitch
statement to detect these different cases. - Acceptable
conjunction
arguments are"and"
,"or"
, and "&"
("ampersand", a symbol meaning "and"). If theconjunction
argument does not match one of these three strings, the function should return a string message that reads"unknown conjunction"
.
If you wish to test your function with your own sample data, call it from the AppDelegate. When the tests in SandboxSpec.swift
all pass you have completed this lab.
View this lesson on Learn.co