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 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,558 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!