Section 3
by Louis Nguyen· Module 1: Engaging with JavaScript Fundamentals

Root Folder
Not Attempted
NameProgress
JavaScript for Beginners Introduction
Not Read
Setting up environment
Not Read
Section 1
Section 2
Section 3
Module 1 Quiz
Not Attempted
Module 2: Introduction to Variables and Data Types
Not Attempted
NameProgress
Section 1
Not Read
Arithmetic Operations
Not Read
Type Conversion
Not Read
Module 3
Not Attempted
NameProgress
Section 1
Not Attempted
Section 2
Not Attempted
0 / 430 XP

Your final task for the first module is to learn to use the window.prompt() method to solicit input from users through a dialog box and handle the input within your JavaScript code.

The window.prompt() displays a dialog box that prompts the visitor for input. It is a part of the Window interface and is used for obtaining user data. The prompt() function lets you display a message to the user and a text box where they can enter a response. The function pauses script execution until the user either submits a response or cancels the prompt.

Step-by-step guide:

  • From the index.html file in section 1, modify the body part by adjusting the script command by the following code:

    <script>
     window.prompt('What is your name? ');
     </script>
  • Save the file and view the changes.
  • The prompt displays a default text 'What is your name? ', under which the user can enter their actual name.

  • You will enter your name in the text box and click OK. However, at this stage, your data is not stored in the console yet.
  • To display in the console, you need to modify the script by assigning the result of the prompt to a variable named username. Then, use console.log()to print a greeting to the console if a name is entered as the following code:

    <script>
     username = window.prompt('What is your name? ');
     console.log(username);
     </script>
  • Save the file again and right-click, choose Inspect, and then navigate to the Console tab.
  • Finally, you should see the name displayed.
Maggie

Discuss with Maggie
Use the power of generative AI to interact with course content

Discussion

0 comments
Loading editor ...
Remember to be polite and report any undesirable behaviour

Category

Empty

Labels

Discussion has no labels

1 participant

user avatar

Priority

Notifications

You're not receiving notifications from this thread.
Course Outline
Module 1: Engaging with JavaScript Fundamentals
Module 2: Introduction to Variables and Data Types
Module 3
Module 4
Module 5