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 trialGina Bégin
Courses Plus Student 8,613 PointsWhy write all the extra steps in there? Seems like they can be condensed even more...
In the video, he's showing us how to combine commands (not sure if that is the right syntax, but basically, combine lines together) to condense our coding. It still seems like it has more lines than necessary, though. I tested the following:
var visitor = prompt("What is your name?");
var message = "Hello " + visitor + ". Welcome to Treehouse. We are so glad that you came by to visit, ";
message += visitor + ". Please come again when you want to learn some more.";
document.write(message);
And got the same result as his code:
var visitor = prompt("What is your name?");
var message = "Hello " + visitor + ". Welcome to Treehouse.";
message += "We are so glad that you came by to visit, ";
message += visitor;
message += ". Please come again when you want to learn some more.";
document.write(message);
Why did he use the longer way?
*Note, you have to scroll to the right to see the full code on my example (the first one); I'm not sure how to code wrap in forums.
4 Answers
Ryan Field
Courses Plus Student 21,242 PointsHi, Gina. When it comes to concatenation of strings, you absolutely can condense your code by avoiding using message += ...
fewer number of times. In a case like this, one or two lines is probably sufficient, but when you start to run into long strings of text stored as variables, using multiple lines makes it so much easier to read. In the end, it's all up to personal preference in this case, though, so you can use as many or as few as you'd like. Though, if you are working on a team, it's always best to follow the established convention so code is consistent for everyone.
Alexander Revell
5,782 PointsI think it's just a taste thing, and having to scroll across is less desirable than being able to read everything in a more even width. You could do it all in one line, but it would be pretty uncomfortable to try and deal with it!
Gina Bégin
Courses Plus Student 8,613 PointsHey Alexander Revell and Ryan Field — so how do I know how many characters are visible before scrolling starts to happen and become uncomfortable to read? In other words, how to do I know when to break to the next line of command so that there isn't any scrolling (if that's the convention)?
Ryan Field
Courses Plus Student 21,242 PointsIt's going to depend on your screen size (width) and the text editor you use. It's basically an eyeball-it kind of thing. :)
Gina Bégin
Courses Plus Student 8,613 PointsThats kinda what I thought, but wondered why others would care about it being best practice if I'm sizing the line breaks for my screen and others' screen sizes might not give them the same result! i.e., They might still need to scroll if their screen size is smaller than mine. (Which isn't likely, unless their monitor is from 1998. ;)
Thanks, though; the explanation makes sense!