Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSebastian Amhøj
280 PointsCan't seem to figure out the last task of "Code Challenge: Formatting Output"
I've been trying to figure this one out for a bit, but can't seem to get it right. So i thought i would seek some help!
The Question is "Use the concatenation operator + to append “ rocks!” to the end of the contents of the firstName variable and print it to the screen using Console.WriteLine"
Please be nice on me :)
string firstName = Console.ReadLine();
Console.WriteLine(firstName);
3 Answers
Steven Parker
231,236 PointsSo starting with the output line you have already:
Console.WriteLine(firstName);
So then if you "Use the concatenation operator + to append " rocks!"
to the end...":
Console.WriteLine(firstName + " rocks!");
Stuart Wright
41,120 PointsI've added an extra line to your code which adds " rocks!" to the end of your variable.
string firstName = Console.ReadLine();
firstName = firstName + " rocks!";
Console.WriteLine(firstName);
Steven Parker
231,236 PointsIf you want to permanently modify firstName
you could do it more concisely with a concatenation assignment operator:
firstName += " rocks!";
Sebastian Amhøj
280 PointsThanks a lot, because of an earlier on the same subject i got confused and used two "+" signs, one on each side :x
Stuart Wright
41,120 PointsSteven - I actually tried that first on the code challenge, thinking it would work, and it failed for me:
string firstName = Console.ReadLine();
firstName += " rocks!";
Console.WriteLine(firstName);
Steven Parker
231,236 PointsFor some reason, the challenge validator doesn't accept that, but it works fine in actual code! But I favor the solution that doesn't modify the variable, since the instructions are a bit ambiguous and the validator accepts it either way..