Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics (Retired) Storing and Tracking Information with Variables The Variable Challenge

Figure out what's wrong please

I have written my line of codes for the variable challenge, it is simple but the dialog box created by the prompt command keeps appearing twice.. Below are the codes written:

console.log("begin the program"); prompt("hey! welcome to my Website.. Thanks for visiting Please what is your name?"); var user_response; user_response = prompt("hey! welcome to my Website.. Thanks for visiting Please what is your name?"); alert(user_response); prompt("Hey!, How old are you?"); var age_declaration; age_declaration = prompt("Hey!, How old are you?"); alert(age_declaration); document.write("Your name is " + user_response + " and you are" + age_declaration ); alert("Thank you for visiting"); console.log("end of the program");

Please can someone help me figure out why they appear twice?

1 Answer

To put it simply, the prompt() command is an alert box that allows the user to store input. It appears, based on what you wrote in the question, that you actually wrote your prompts twice. You only need to store them each ONCE to store them in the catch vars you used (user_response, age_declaration).

    console.log("begin the program");
    var user_response;
    user_response = prompt("Hey! welcome to my Website.. Thanks for visiting Please what is your name?");
    alert(user_response);
    var age_declaration;
    age_declaration = prompt("Hey!, How old are you?");
    alert(age_declaration);
    document.write("Your name is " + user_response + " and you are" + age_declaration);
    alert("Thank you for visiting");
    console.log("end of the program");

Use this for reference. This should behave how you want it to. Just remember you only need to declare these prompt() commands ONCE and that ONCE should be where you store the variable, given that you want to record the information.

If you run your code before using what I provided, try recording different names (I.E. Bob then Bill). Bill will get written and Bob will be forgotten. Same thing with applies to age; if you wtie 36 and 13, 36 will get dropped and 36 will be recorded isntead.

Hope this helps.

Thanks so much.. I can spot the difference... You made my day.