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

What is wrong with my code. It dosen't display in h2. If I'm removing h2 the script is running.

var sport = prompt('What is your favorite sport');
var answer = "<h2> sport + ' is your favorite sport. ';
var player = prompt('What is your favorite player');
var shot = prompt('And which is your favouritr shot');
alert('All done. Here are your result');
answer += player + ' is your favorite player. ';
answer += shot + ' is your favorite shot. </h2>";
document.write(answer);

3 Answers

Sean T. Unwin
Sean T. Unwin
28,690 Points

Hi Reno Pahnke,

As you can see from my editing of your question to display the code in a more readable fashion, there is a closing quote missing in var answer. There should be a double quote directly after the <h2>. Furthermore, there should be a plus sign (+) following that, yet before sport.

//                v-- missing `" + `
var answer = "<h2> sport + ' is your favorite sport. ';
// Should be 
var answer = "<h2>" +  sport + ' is your favorite sport. ';

And

//               v-- single quote      ...     v-- double quote 
answer += shot + ' is your favorite shot. </h2>";
// Should be
answer += shot + ' is your favorite shot. </h2>';

TIP: Be careful when mixing single and double quotes and always be sure to close them when required.

thank's :)

Steven Parker
Steven Parker
229,744 Points

Each string must have matched pairs of quotes, you have two lines with unmatched quotes. Here they are fixed:

var answer = '<h2>' + sport + ' is your favorite sport. ';
//...
answer += shot + ' is your favorite shot. </h2>';

Also, you might find it easier to keep track of the quotes if you always use the same kind, as I have done here.

thank's :)

thank's for solving that :)