Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics (Retired) Storing and Tracking Information with Variables The Variable Challenge Solution

Why did my code run properly when I programmed it like this?

console.log('Start');
var questionOne = prompt('What\'s the sky\'s color right now?');
var completeStory = 'I looked outside and saw the sky was ' + questionOne;
var questionTwo = prompt('Write either walk or run');
completeStory += '. So I decided to ' + questionTwo;
var questionThree = prompt('Choose a different color');
completeStory += ' to the store today.'
alert('You are all done. Let\'s read your completed story now.');
document.write(completeStory);
console.log('End');

Versus when I programmed it like this:

console.log('Start');
var questionOne = prompt('What\'s the sky\'s color right now?');
var questionTwo = prompt('Write either walk or run');
var questionThree = prompt('Choose a different color');
var completeStory = "I looked outside and saw that the sky was " + questionOne + ". " "So I decided to " questionTwo + " to the store today. " + "Little did I know the sky was falling and actually the color was " + questionThree + " Moral of the story think twice before you act.";
alert('You are all done. Let\'s read your completed story now.');
document.write(completeStory);
console.log('End');

I greatly appreciate the feedback!

2 Answers

andren
andren
28,503 Points

The issue is in this line:

var completeStory = "I looked outside and saw that the sky was " + questionOne + ". " "So I decided to " questionTwo + " to the store today. " + "Little did I know the sky was falling and actually the color was " + questionThree + " Moral of the story think twice before you act.";

You are missing a + after the ". " and "So I decided to " strings. If you add them like this:

var completeStory = "I looked outside and saw that the sky was " + questionOne + ". " + "So I decided to " + questionTwo + " to the store today. " + "Little did I know the sky was falling and actually the color was " + questionThree + " Moral of the story think twice before you act.";

Then your code will work. Missing a + or two when concatenating many strings in a row is a somewhat common typo which causes a syntax error, which means that your script will stop running.

Thanks for the help!