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 trialJessica Andhika
Courses Plus Student 413 PointsHow do I insert a space in a variable? e.g. var1 + " " + var2 I tried putting but it didnt show up..
This is my sample workspace..
var firstName = prompt("What's your name?");
var favAnimal = prompt("Name your favourite animal.");
var adjective = prompt("Describe yourself in 1 word.");
var hobby = prompt("Name your favourite hobby.");
var verb = prompt("What do you feel like doing right NOW?");
var noun = prompt("What is your favourite meat?");
var story = "<h2>There was once a " + adjective + " " + favAnimal + "named " + firstName + ".</h2>";
document.write(story);
The " " did not appear, so the sentence became e.g. There was once a cute turtlenamed Eric. Thank you.
7 Answers
jason chan
31,009 Pointsvar firstName = prompt("What's your name?");
var favAnimal = prompt("Name your favourite animal.");
var adjective = prompt("Describe yourself in 1 word.");
var hobby = prompt("Name your favourite hobby.");
var verb = prompt("What do you feel like doing right NOW?");
var noun = prompt("What is your favourite meat?");
var story = "<h2>There was once a " + adjective + " " + favAnimal + " named " + firstName + ".</h2>";
document.write(story);
refactored try now!
Samantha Inmon
5,127 PointsIn addition to Jason's comment, to separate the words "turtle" and "named", you just need to write a space inside the quotes before the word like this:
favAnimal + " named " + firstName + rest of code...
jason chan
31,009 Pointsvar firstName = prompt("What's your name?");
var favAnimal = prompt("Name your favourite animal.");
var adjective = prompt("Describe yourself in 1 word.");
var hobby = prompt("Name your favourite hobby.");
var verb = prompt("What do you feel like doing right NOW?");
var noun = prompt("What is your favourite meat?");
var story = "<h2>There was once a " + adjective + " " + favAnimal + "named " + firstName + ".</h2>";
document.write(story);
You can leave the spaces as space in quotes. You don't need the html entity. That's only for HTML.
Jessica Andhika
Courses Plus Student 413 Pointshey guys, thanks for the prompt help, but the space I need is between the variables adjective and favAnimal. I've also tried adding just blank space, not the HTML, and it didn't show up...
Idan Melamed
16,285 PointsHi Jessica If you want to add a space, you can put quotes around a space, like this:
" "
So in your example you would do:
adjactive + " " + favAnimal
Richard Santana
5,305 Points// This works , I tested it three times
var firstName = prompt("What's your name?"); var favAnimal = prompt("Name your favourite animal.")+ " "; var adjective = prompt("Describe yourself in 1 word."); var hobby = prompt("Name your favourite hobby."); var verb = prompt("What do you feel like doing right NOW?"); var noun = prompt("What is your favourite meat?"); var story = "<h2>There was once a " + adjective + " " + favAnimal + "named " + firstName + ".</h2>"; document.write(story);
// I added a space --> var favAnimal = prompt("Name your favourite animal.")+ " "; <--
Jessica Andhika
Courses Plus Student 413 Pointsthanks everyone :)