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 trialRamiro Blanco
7,266 PointsHere's my version if anyone is stuck
var carryOn = "y"; var story = "";
while (carryOn == "y") { story += prompt("Add a word to your story") + " "; carryOn = prompt("If you want to add another word to your story, enter \"y\"."); }
document.write("Your story is: " + story.trim() + ".");
4 Answers
Marcus Parsons
15,719 PointsHey Ramiro,
I see what you did there, but you can be quite a bit more efficient in your code. A user wouldn't like to have to keep pressing y every time they want to add another word to their story. It would make for a very long and annoying experience. I changed your code around quite a bit so that when you run this, you can just press enter or cancel to break out of the prompt and display the story.
var story = "";
while (true) {
var partStory = prompt("Add a word/some words to your story or just press enter to cancel.");
//if they press enter or they press cancel, break out of while loop
if (partStory === "" || partStory === null) { break; }
//Otherwise, add to the story
story += partStory + " ";
}
document.write("Your story is: " + story.trim() + ".");
Kelso Kosa
256 PointsThis really isn't helpful to beginners.
Marcus Parsons
15,719 PointsAre you talking about my code? lol.
Kelso Kosa
256 PointsThe overall scope.
pablocubillos
10,047 PointsBetter a beginners 3-word story:
var firstNoun = prompt ("say a noun"); var firstVerb = prompt ("say a verb"); var firstAdjective = prompt ("say an adjective"); alert ("Are you done?"); var completeSentence = firstNoun +" "+ firstVerb +" "+ firstAdjective; document.write ("You said: " + completeSentence); console.log (completeSentence);