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 JavaScript Basics (Retired) Storing and Tracking Information with Variables The Variable Challenge Solution

Would this work too?

This is what I coded:

//Prompt word collecting
var noun = prompt("Give me a noun!");
var verb = prompt("Give me a verb!");
var adjective = prompt("Give me an adjective!");

//Sentance variables
var sentance1 = "There once was a";
var sentance2 = "programmer who used JavaScript to";
var sentance3 = "the";

//Make the full sentance
document.write(sentance1 + ' ' + noun + ' ' + sentance2 + ' ' + verb + ' ' + sentance3 + ' ' + adjective);

It worked. I found it a bit simpler. This would work the same way as well, right?

2 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

Hi David,

I put some forum markdown code on your post just to make the code easier to read.

You're right though you've found another way to do the challenge, and that's great. There's more than one way to solve every problem in JavaScript and the same goes for any language. Just go with whatever works for you and try not to bet hung up doing it one particular way. :-)

Norbu Wangdi
Norbu Wangdi
7,187 Points

document.write(sentance1 + ' ' + noun + ' ' + sentance2 + ' ' + verb + ' ' + sentance3 + ' ' + adjective); Can somebody explain me this piece of code? what does those empty single quote mean? is there other ways to write this?

The empty singles quotes they're using are meant to add a space in between the "a" (in "There once was a"; ) & the noun, in between the "to" (in "programmer who used JavaScript to") & the verb, and in between the "the" (in "the") & the adjective.

Without the spaces the sentence would look like this:

There once was anoun programmer who used JavaScript toverb theadjective

It is unnecessary to do it that way, but it still works. An easier way to write it would be to add the space after "a", "to", and "the" like this:

"There once was a "; "programmer who used JavaScript to "; "the ";

Then the document.write would look like this:

document.write(sentance1 + noun + sentance2 + verb + sentance3 + adjective);