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 script?

I want to create a prompt that allows people to enter where they are going to travel for the summer, and then create statements based off that answer using javascript.

After the person enters their answer in the prompt or alert box, say London, I would then hope to produce a statement that say: 'Welcome to London. I hope you have a wonderful time. Stay as long as you can.'

Also, I get in the console this error in regards to my +=. "SyntaxError: Unexpected token '+='. Expected ';' after var declaration." What does this mean?

console.log('The journey begins'); 
var travel = prompt('Where are you going this summer?'); 
document.write(travel);
var travel += "Welcome to" + travel ;
var travel = travel + " I hope you have a wonderful time.";
var travel = travel + " Stay as long as you can.";
document.write(travel);

edited code for formatting.

1 Answer

Hi Lorraine,

First, I see you declared the variable "travel" on the 4th, 5th and on the 6th lines. No point in declaring it again on the next lines. When using a variable that had already been declared - you should not write "var" in the begining of the line.

Second, when you concatenate using "+=", you are adding an existing value in the variable to the value after the equal sign. But on your fourth line of code [var travel += "Welcome to" + travel], you added the value again after the equal sign.

Third - no need to write to write the "travel" value to the document - since you're writing the long greeting in the end of the code. I'm not sure why you put this line in there [document.write(travel);]

If it was my code, I would create a different variable for the long greeting - just to keep things simple:

console.log('The journey begins'); 
var greeting;
var travel = prompt('Where are you going this summer?'); 
greeting = "Welcome to " + travel +". I hope you have a wonderful time. Stay as long as you can."; 
document.write(greeting);

I hope this helped. Feel free to ask me if anything isn't clear. Michal

Thank you. I'll rewrite it.