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 trialMile Mijatovic
7,792 PointsNeed help about JavaScript Basic challenge 2 of 3
I am newbie about javascript, please help me. Challenge is - Use the prompt() method to ask the user "What day is it?" and store the result in the answer variable.
var day = prompt('What day is it?'); var answer = (I dont know what to type here);
7 Answers
Erik McClintock
45,783 PointsMile,
You're close! This one is easy to get confused on, based on how prompt() works, and on how the task is worded.
If you recall, whatever a user enters into the prompt when it pops up is the value that will be stored in the variable that the prompt is assigned to, so you actually need to write less than what you've got!
You need to create a variable called 'answer', and assign the prompt to that variable, so that the value given to the prompt will be stored in that variable.
var answer = prompt('What day is it?');
// to test this out and see the result, you can add this console.log statement, copy and paste all of this into your dev tools, and run it
console.log(answer);
Hope this helps to make a little bit more sense of things!
Erik
Mile Mijatovic
7,792 PointsOops, something was wrong. My first line - var day = prompt('What day is it?'); My second line - var answer = prompt('What day is it?');
And what's happened - I have two dialog box in row with question What day is it? But I don't need, I want to display answer as dialog box when I write a day.
Erik McClintock
45,783 PointsMile,
You only want the one line, not two. Writing the following takes care of everything for you:
var answer = prompt('What day is it?');
That line creates a variable in which the user's answer to your prompt will be stored. You do not need to create a second variable or a second prompt; that is the only thing you need.
Erik
Josip Dorvak
18,126 PointsThe prompt function RETURNS the value the user types in to the prompt. So to store the the users response in the answer variable, switch day with answer like so:
var answer = prompt('What day is it?');
Mile Mijatovic
7,792 PointsChallenge 3 of 3 Use the document.write() method to write the variable answer to the page.
Josip Dorvak
18,126 Pointsvar answer = prompt('What day is it?'); //This will get the answer
//then to display the answer
document.write(answer);
you do not need the day variable at all, since answer is what is holding the day
Erik McClintock
45,783 PointsIf you need to then use the document.write() method to write the variable answer to the page, do just that! Beneath your prompt line in your code, call document.write() and pass in the "answer" variable as the parameter for the method.
Erik
Mile Mijatovic
7,792 PointsThanks guys for fast reply ;)
Deric cahill
2,805 PointsYou guys are all my heroes.
I was doing 2 lines and was about to lose my mind haha.
I kept coding like
var answer = "What day is it?"; prompt("What day is it");
Ioana Raluca Darie
3,540 Pointsso ... my first answer was : var answer = prompt('what day is it ?'); and i receive the message : Bummer! You didn't put 'What day is it? in the prompt()
function. but the dialog box shows on the screen with the question "what day is it ?"
my bad ? or .... i do not understand
Josip Dorvak
18,126 PointsI think your answer is fine, the problem with treehouse is they want strings to be exactly the same. Make the 'w' in 'what' a capital and it should be good.
mohammed mayat
4,228 Pointsmohammed mayat
4,228 Pointsvar answer prompt=("What day is it?"); prompt=("answer");
i am stuck on this challenge need help