1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140# High School Sweetheart
Welcome to High School Sweetheart 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
## Strings
Strings in Elixir are delimited by double quotes, and they are encoded in UTF-8:
```elixir
"Hi!"
```
Strings can be concatenated using the `<>/2` operator:
```elixir
"Welcome to" <> " " <> "New York"
# => "Welcome to New York"
```
Strings in Elixir support interpolation using the `#{}` syntax:
```elixir
"6 * 7 = #{6 * 7}"
# => "6 * 7 = 42"
```
To put a newline character in a string, use the `\n` escape code:
```elixir
"1\n2\n3\n"
```
To comfortably work with texts with a lot of newlines, use the triple-double-quote heredoc syntax instead:
```elixir
"""
1
2
3
"""
```
Elixir provides many functions for working with strings in the `String` module.
## Pipe Operator
The `|>` operator is called the pipe operator. It can be used to chain function calls together in such a way that the value returned by the previous function call is passed as the first argument to the next function call.
```elixir
"hello"
|> String.upcase()
|> Kernel.<>("?!")
# => "HELLO?!"
```
## Instructions
In this exercise, you are going to help high school sweethearts profess their love on social media by generating an ASCII heart with their initials:
```
****** ******
** ** ** **
** ** ** **
** * **
** **
** J. K. + M. B. **
** **
** **
** **
** **
** **
** **
***
*
```
## 1. Get the name's first letter
Implement the `HighSchoolSweetheart.first_letter/1` function. It should take a name and return its first letter. It should clean up any unnecessary whitespace from the name.
```elixir
HighSchoolSweetheart.first_letter("Jane")
# => "J"
```
## 2. Format the first letter as an initial
Implement the `HighSchoolSweetheart.initial/1` function. It should take a name and return its first letter, uppercase, followed by a dot. Make sure to reuse `HighSchoolSweetheart.first_letter/1` that you defined in the previous step.
```elixir
HighSchoolSweetheart.initial("Robert")
# => "R."
```
## 3. Split the full name into the first name and the last name
Implement the `HighSchoolSweetheart.initials/1` function. It should take a full name, consisting of a first name and a last name separated by a space, and return the initials. Make sure to reuse `HighSchoolSweetheart.initial/1` that you defined in the previous step.
```elixir
HighSchoolSweetheart.initials("Lance Green")
# => "L. G."
```
## 4. Put the initials inside of the heart
Implement the `HighSchoolSweetheart.pair/2` function. It should take two full names and return the initials inside an ASCII heart. Make sure to reuse `HighSchoolSweetheart.initials/1` that you defined in the previous step.
```elixir
HighSchoolSweetheart.pair("Blake Miller", "Riley Lewis")
# => """
# ****** ******
# ** ** ** **
# ** ** ** **
# ** * **
# ** **
# ** B. M. + R. L. **
# ** **
# ** **
# ** **
# ** **
# ** **
# ** **
# ***
# *
# """
```
## Source
### Created by
- @angelikatyborska
### Contributed to by
- @neenjaw