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

Greg Kitchin
Greg Kitchin
31,522 Points

JavaScript Variable Challenge, my own code not working

Hi. No idea if I'm missing something obvious or not, but while doing the variable challenge, my code doesn't seem to run at all. If I only have the first line, it will run, but if I add another (or the rest of my code), no prompts will launch. Can anyone see any problems with my code?

var adjective = prompt('Please type an adjective');
var verb = prompt('Please type an verb');
var noun = prompt('Please type a noun');
var sentance += "<h2> There once was a " + adjective;
sentance += ' programmer who wanted to use JavaScript to ' + verb;
sentance += '  save the ' + noun + '.</h2>
document.write(sentance);

4 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Greg,

You have a couple of syntax errors in your code.

  1. The first is you have var sentance +=, the += is invalid for variable declarations, instead it should just be =
  2. you haven't closed off your last sentance string where your closing H2 element is
var adjective = prompt('Please type an adjective');
var verb = prompt('Please type an verb');
var noun = prompt('Please type a noun');
var sentance = "<h2> There once was a " + adjective;
sentance += ' programmer who wanted to use JavaScript to ' + verb;
sentance += '  save the ' + noun + '.</h2>';
document.write(sentance)

Happy coding!

Mohsin Ayub
Mohsin Ayub
6,822 Points

When first declaring a variable, only use = operator. You make the mistake while declaring sentence variable, use the following syntax:

var sentence = "<h2> There once was a " + adjective;

then use the += operator to concatenate the strings

Zach Saul
Zach Saul
11,156 Points

hm. without knowing what your task was it's tough for me to help you - but if I had to guess - I'd say your problem is coming from that 6th line of yours: (sentance += ' save the ' + noun + '.</h2>)

couple issues: 1) you have three quotations which doesn't make sense - depending on what you're intending to write, you'll either need 2 of 4, and if you need to use multiple sets the best way to solve this problem is to use single quotes on one phrase, and double on the other: so for example : if you wanted to say sentance +=' save the' "+noun +".</h2> would be one way around it - again I'd need more information about what yor trying to accomplish to be of more assistance.

2) depending on what you are trying to say you can use escape characters if your apostrophe of quotation mark is being misinterpreted by javascript (which might be happening) by placing a \ before your ' so the way that might look:

sentance += \ ' save the ' + noun + '.</h2>

again - for me to help you I'd need more information about what you are tying to accomplish but i hope this helps!

Greg Kitchin
Greg Kitchin
31,522 Points

Thanks for that. The variable declaration for sentance and the missing quote from the last line was indeed the problem. Seems a bit strange though that none of the prompt's were popping up. Is that due to the workstation? I'd have thought that since those lines were find, then they would have worked and the last half of the code just wouldn't execute or throw an error.