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 trialstefanides
721 PointsMy code is working, but i don't store sentence in variable
var adjective = prompt("Lorem"); var verb = prompt("Ipsum"); var noun = prompt("Dolor");
alert("Done");
document.write("This " + adjective + " is a dummy" + verb + " text" + noun);
Everything is working, why should i put sentence into a new variable and add 2 lines of new code ( += "text" like this one) if i can run everything in one line of code in document.write?
Thank you!
4 Answers
Siddharth GS
2,698 PointsHello Denis Stefanides ,
To make it into a better way to write this program - try like our professor said - I did my very first own code as he taught. Split up your code to make it easy and understandable -> It's awesome. Check ma code - i hope it helps
console.log("Begin Program");
var name = prompt("What is your name?");
var age = prompt("What is your age?");
var ambition = prompt("What is your ambition?");
var food = prompt("What is your favorite food?");
var future = prompt("Where you wanna see yourself in the future?");
alert("Are you sure that you've finished with your story?");
var result = " My name is " + name;
result += " and I'm" + age + "years old.";
result += " My ambition is to become a" + ambition;
result += " My favorite food is " + food;
result += " and I want to see myself as " + future.toUpperCase();
document.write(result);
console.log("End Program");
David Bath
25,940 PointsThe short answer: you don't need to! The only reason you might want to do that is for clarity, so that the sentence is constructed outside of the document.write function, and then supplied as an argument to it. Part of becoming a good programmer is writing code that is easy for other developers to read and understand.
Damien Watson
27,419 PointsHey Denis, A lot of the time the teachers are teaching the fundamentals (eg. variable concatenation). Sometimes you may need to store your output in a variable, other times you can just dump it out as you've suggested.
The other thing about coding is that there are usually many ways to achieve the same result. The only way to learn these ways is to go through them, you can then see what works best for your circumstance or taste.
stefanides
721 PointsThank you guys, really appreciate your help :)
Steve Tenpas
6,234 PointsSteve Tenpas
6,234 PointsSiddharth GS, This is a great example. I had the same question as Denis. I also used numerous variables in my challenge but put it all into the document.write function. I can see now how clear this is to understand the code when it is structured this way. This would seem to make edits easier. Thanks for taking the time to show your example. Helpful.