Danil Negrienko 7332230435 | ||
---|---|---|
.. | ||
.exercism | ||
lib | ||
test | ||
.formatter.exs | ||
.gitignore | ||
HELP.md | ||
HINTS.md | ||
README.md | ||
mix.exs |
README.md
Name Badge
Welcome to Name Badge on Exercism's Elixir Track.
If you need help running the tests or submitting your code, check out HELP.md
.
If you get stuck on the exercise, check out HINTS.md
, but try and solve it without using those first :)
Introduction
Nil
Nil is an English word meaning "nothing" or "zero". In Elixir, nil
is a special value that means an absence of a value.
# I do not have a favorite color
favorite_color = nil
In other programming languages, null
or none
values might play a similar role.
If
Besides cond
, Elixir also provides the macro if/2
which is useful when you need to check for only one condition.
if/2
accepts a condition and two options. It returns the first option if the condition is truthy, and the second option if the condition is falsy.
age = 15
if age >= 16 do
"You are allowed to drink beer in Germany."
else
"No beer for you!"
end
# => "No beer for you!"
It is also possible to write an if
expression on a single line. Note the comma after the condition.
if age > 16, do: "beer", else: "no beer"
This syntax is helpful for very short expressions, but should be avoided if the expression won't fit on a single line.
Truthy and falsy
In Elixir, all datatypes evaluate to a truthy or falsy value when they are encountered in a boolean context (like an if
expression). All data is considered truthy except for false
and nil
. In particular, empty strings, the integer 0
, and empty lists are all considered truthy in Elixir.
Instructions
In this exercise you'll be writing code to print name badges for factory employees. Employees have an ID, name, and department name. Employee badge labels are formatted as follows: "[id] - name - DEPARTMENT"
.
1. Print a badge for an employee
Implement the NameBadge.print/3
function. It should take an id, name, and a department. It should return the badge label, with the department name in uppercase.
NameBadge.print(67, "Katherine Williams", "Strategic Communication")
# => "[67] - Katherine Williams - STRATEGIC COMMUNICATION"
2. Print a badge for a new employee
Due to a quirk in the computer system, new employees occasionally don't yet have an ID when they start working at the factory. As badges are required, they will receive a temporary badge without the ID prefix.
Extend the NameBadge.print/3
function. When the id is missing, it should print a badge without it.
NameBadge.print(nil, "Robert Johnson", "Procurement")
# => "Robert Johnson - PROCUREMENT"
3. Print a badge for the owner
Even the factory's owner has to wear a badge at all times. However, an owner does not have a department. In this case, the label should print "OWNER"
instead of the department name.
Extend the NameBadge.print/3
function. When the department is missing, assume the badge belongs to the company owner.
NameBadge.print(204, "Rachel Miller", nil)
# => "[204] - Rachel Miller - OWNER"
Note that it is possible for the owner to also be a new employee.
NameBadge.print(nil, "Rachel Miller", nil)
# => "Rachel Miller - OWNER"
Source
Created by
- @angelikatyborska
Contributed to by
- @neenjaw