Exercise: Template Strings
Observation: In the concept, we are shown a right example of the usage of
When you are needing to have strings formatted on multiple lines:
`This is an example of using template
strings to accomplish multiple
lines`;
However, in the 3rd task of custom-signs, we see:
Implement the function graduationFor(name, year) which takes a name as a string parameter and a year as a number parameter and uses string interpolation to create a phrase for a sign that uses a newline to separate the two lines of the message.
graduationFor('Hannah', 2022);
// => "Congratulations Hannah!\nClass of 2022"
Issue: I think the example on when needing to have strings formatted on multiple lines can be improved to include the usage of the newline escape sequence, and or the output // => "Congratulations Hannah!\nClass of 2022" shouldn’t contain the sequence, rather it should be like
// => "Congratulations Hannah!
Class of 2022"
The person solving it would know to format it that way, rather than putting the exact sequence in the output as may think that that exact string \n is to be included in the output, thereby escaping the backslash.
. What do you think?
cc: @SleeplessByte
1 Like
I also understand that there might be a misunderstanding that one needs to print two lines, rather than using the escape sequence itself if it were shown thus:
graduationFor("Hannah", 2022);
// => Congratulations Hannah!
// Class of 2022
then a beginner might think they are expected to literally print two lines separately (e.g., with two console.log() calls), instead of returning one string that contains the \n escape sequence.
The safest way is to show two forms side by side:
- The string value (how it looks in code, with
\n escape sequence).
- The rendered output (how it appears when printed).
graduationFor("Hannah", 2022);
// Returned string:
"Congratulations Hannah!\nClass of 2022"
// Printed result:
Congratulations Hannah!
Class of 2022
What do you think?
Since the goal is to teach string interpolation, both would be fine as implementation.
It’s hard to test for specific use (hard but possible) and I don’t want to force it onto people.
Adding an example like string with newline and template string with a new line is good idea. You can PR that if you’d like.
1 Like
I have submitted a PR here.
1 Like
I think there’s an issue in the PR. Let me double check
I solved the problem in task 3 in my PR! :))
@SleeplessByte Scratch that PR, let me redo it, including the task 3 edit.
Alright, I have made edits correctly, kindly review: Here
You may ignore PR #2768, thanks.
2 Likes