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 trialNir Frank
3,578 PointsThe Variable Challenge - My Solution
Complete beginner, only learned HTML & CSS beforehand. I'm doing the JavaScript Basics course, and this is my Variable Challenge solution:
var story = "There once was a ";
var adjective = prompt("Please provide an adjective:");
story += adjective.toUpperCase() + " programmer who wanted to ";
var verb = prompt("Please provide a verb:");
story += "<i>" + verb + "</i> JavaScript in order to change the ";
var noun = prompt("Please provide a noun:");
story += "<b>" + noun.toLowerCase() + "</b>.";
alert("You're done! Ready for your story?");
document.write("<p>" + story + "</p>");
document.write("<p><i>By the way, your story is <b>" + story.length + "</b> characters long! Wow :)</i></p>");
Thoughts? Advice? Criticism? What can I do to make the code better? Also, I came to realize that the .length property (is it called a property? what's the correct term for a .anything?) also counts html markup and such - is there a way to ignore those characters?
1 Answer
Marcus Parsons
15,719 PointsHiya Nir Frank,
That's a nice way of doing that challenge. You can't really get better code per se than that because the challenge is so simple.
In regards to your questions, usually when you see a "." and then a name, it will be a method or a property. "length" is a property because it can be attached to any JavaScript variable and return its length. What it returns is based upon what that variable is, though. For a string, it counts the total number of characters. For an array or an object, it returns the total number of indices or key/value pairs, respectively.
Also, if you wanted to get the length of the characters in the string, you can use a regular expression to do that:
story.replace(/\s/g, "").length
Look up regular expressions for more info on them! If you have any more questions, I'm here to help!