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 Capturing Visitor Input and Writing It to the Page

Emily V
Emily V
8,553 Points

Needing help - Use the document.write() method to write the variable answer to the page

Not sure what I am doing wrong on this one. Thanks for any help!

scripts.js
var answer = prompt('What day is it?');
prompt ('What day is it?')
document.write(answer);
index.html
<!DOCTYPE HTML>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>JavaScript Basics</title>
</head>
<body>
<script src="scripts.js"></script>
</body>
</html>

2 Answers

Kieran Barker
Kieran Barker
15,028 Points

This is what your code should be:

var answer;
answer = prompt('What day is it?');
document.write(answer);
  1. The first line declares the answer variable without storing anything inside it.
  2. The next line then updates the answer variable by storing inside it the string entered into the prompt (the first and second line could be shortened to var answer = prompt('What day is it?') but that isn't what this challenged has asked you to do).
  3. The last line writes the string stored inside the answer variable to the page.
Billy Peacock
Billy Peacock
8,089 Points

var answer = prompt('What day is it?');

prompt ('What day is it?')

document.write(answer);

You don't need the second line as you have already declared the variable.

The first line sets the var answer to the prompt, then all you need to so is document.write and thats it!

var answer = prompt('What day is it?');

document.write(answer);

Kieran Barker
Kieran Barker
15,028 Points

You’re right, but Task 1 of this challenge asks you to declare a variable without initialising it, then in Task 2 you update the contents of the variable, hence why Emily has tried to do it across two lines.