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

brendon hanson
brendon hanson
6,191 Points

Why Isn't it working! Variable challenge

Here is my code var adjective = prompt('Type an adjective'); var answer1 = adjective; var noun = prompt('Type a Noun'); var answer2 = noun; var verb = prompt('Type a verb'); var answer3 = verb; document.write('Hi, I am an alien from the the great planet of merso! I am very' + ' answer1' + 'I hope this does not scare you. My family lives in a' + ' answer2' + 'It is usually crowded so I find myself' + ' answer3'); Instead of the program showing the answer that the user types it literally just says answer1 Please HELP!

2 Answers

P M
P M
3,672 Points

You have enclosed the variables in quotes. While concatenating the variables do not need quotes.

var adjective = prompt('Type an adjective'); var answer1 = adjective; var noun = prompt('Type a Noun'); var answer2 = noun; var verb = prompt('Type a verb'); var answer3 = verb;

document.write('Hi, I am an alien from the the great planet of merso! I am very ' + answer1 + ' I hope this does not scare you. My family lives in a ' + answer2 + ' It is usually crowded so I find myself ' + answer3);

brendon hanson
brendon hanson
6,191 Points

Thanks, for some reason I keep making that mistake

Zachary Berling
Zachary Berling
10,703 Points

/* Fixed your code to make it a bit more easy to read and created the spacing that it would need in order to flow better. Used html h1 h2 h3 tags for each question. */

var adjective = prompt("Type an adjective"); var answer1 = adjective;

var noun = prompt("Type a Noun"); var answer2 = noun;

var verb = prompt("Type a verb"); var answer3 = verb;

document.write("<h1>Hi, I am an alien from the the great planet of Merso!</h1> <h2>I am very " + answer1 + " I hope this does not scare you.</h2> <h3>My family lives in a " + answer2 + " It is usually crowded so I find myself " + answer3 + "...</h3>" );

/* Made a slightly better version of the code above as well using just the h1 html tag and created a new variable which would use all 3 questions to be used for document.write(). Came up with a interesting alert as well :D*/

var adjective = prompt("Type an adjective"); var answer1 = "<h1> Hi, I am an alien from the the great planet of Merso! I am very " + adjective;

var noun = prompt("Type a Noun"); var answer2 = " I hope this does not scare you. My family lives in a " + noun;

var verb = prompt("Type a verb"); var answer3 = " It is usually crowded so I find myself " + verb + '</h1>';

alert("Beaming up story now!")

var story = answer1 + answer2 + answer3

document.write(story)