Take your name and the name of another student in the group and create a combined name
Create a collection containing your full name and the name of another person (or two imaginary people if you prefer)
Give your collection a name so you can use it in later exercises on this page.
()
(def students ["Ada Lovelace" "Anne-Marie Imafidon"])
Assuming there is a first and last name for each person we need to split them into individual strings first.
Write a function call to split each name in your
students
collection
()
(clojure.string/split students #" ")
;;
;; => ["Ada" "Lovelace" "Anne-Marie" "Imafidon"]
In JavaScript the above gives a rather messy output of nested collections.
Mapping over the student collection gives cleaner return value
(map #(clojure.string/split % #" ") students)
We can also flatten the result to make it look nicer
(flatten (map #(clojure.string/split % #" ") students))
clojure.string/split function will split a string on a given pattern (regular expression), such as a space:
#" "
.
Write a function called
name-split
that a full name as a string and return two seperate strings, one for the first name and one for the last name.
()
(defn name-split
"Splits a name into first & last names"
[name]
(clojure.string/split name #" "))
;;
;; (name-split "Ada Lovelace")
;; =>["Ada" "Lovelace"]
Write a function to jumble up the first and last names to create a new person
For example, take the first name from the first person and join it with the last name from the second person
()
(defn jumble-names [names]
(let [first-person (first names)
second-person (second names)
first-person-first-name (first (name-split first-person))
second-person-second-name (second (name-split second-person))]
(str "Hello " first-person-first-name second-person-second-name)))
;;
(jumble-names students)
;; => "Hello AdaImafidon"
Remember, the
let
function defines local names that you can use for temporary values. Using several names with thelet
function can make this challenge a little simpler to solve