JavaScript JavaScript Basics Storing and Tracking Information with Variables The Variable Challenge

brendon hanson
brendon hanson
1,537 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
2,842 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
1,537 Points

Thanks, for some reason I keep making that mistake

Zachary Berling
Zachary Berling
6,892 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)