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 trial

JavaScript The Solution

I would like feedback on my code, if possible please!

Hey everybody! I just finished working on this practice code for this workshop, and I'm quite proud that I got to work without looking up the answer. However, I have a question. I have seen some other people doing it a different way, and was basically just asking: What would be the BEST practice way to do this? Here is my code:

var name = prompt("What is your first name?");

var lastName = prompt("What is your last name?");

var fullName = name +" " + lastName;

console.log(fullName);

var lengthOfString = fullName.length;

console.log(fullName + " is " + lengthOfString + " characters long" );

@Michael Brown this was my solution:

var firstName = prompt("Please enter your first name:");
console.log('First Name: ', firstName);

var lastName = prompt("Please enter your last name:");
console.log('Last Name: ', lastName);

var fullNameInCaps = (firstName + " " + lastName).toUpperCase();
console.log('FULL NAME: ', fullNameInCaps);

var charLength = fullNameInCaps.length;

alert("The string '" + fullNameInCaps + "' is " + charLength + " number of characters long.");

3 Answers

Steven Parker
Steven Parker
231,236 Points

If you have a question about a particular difference, post the other version for comparison.

Otherwise, one minor point is that you might want to avoid creating variables that are only used one time. So, for example, if "lengthOfString " is not used again after the message is logged, you could skip creating it and output the message like this:

console.log(fullName + " is " + fullNameInCaps.length + " characters long" );
Steven Parker
Steven Parker
231,236 Points

Your code seems functionally identical to the "official" solution except for the clever way you managed to call "toUpperCase" only one time. :+1:

Would it do the job?

const firstName = prompt('What is your first name?').toUpperCase();

const secondName = prompt('What is your second name?').toUpperCase();

alert(The length of your full name is: ${firstName.concat(' ', secondName).length} characters long.);

I like it. As long as it works it's fine. I did something different using MDN.com. it is more condensed.

const firstName = prompt('What is your first name?').toUpperCase(); const secondName = prompt('What is your second name?').toUpperCase(); alert(The length of your full name is: ${firstName.concat(' ', secondName).length} characters long.);

That means so much, @Steven! Thank you so much for the quick feedback -- definitely keeps me motivated on my journey with coding and thinking like an engineer!