Below is an example of a simple function that can be used to pull up a prompt message that asks the user to “Enter a single digit number:”.
numberx = prompt(“Enter a single digit number: “,0);
This is an extremely easy function to write and understand. The function name is called ‘prompt_one()’. You do not need to have any data called as arguments. This is shown because there is nothing in the function parameters, or what is between the parenthesis. The reason for this function is to prompt the user to ‘Enter a single digit number’ and store that number as variable ‘numberx’. Also, the default value set in the prompt will be 0.
In order to call this function, you will need to use some HTML. If you have ever used forms, you are in luck. Forms can be used to call the functions in a very simple way. Below is the HTML that is used to call the function ‘prompt_one()’. This will need to be done within a form tag and within the tags.
input type=”button” onclick=”prompt_one()” value=”Enter A Single Digit Number”
As you can see, in order to create a form, you need to use the form tags. Inside these tags, you need to declare the input type. I have decided to use a button input type. Here is where the magic happens. in the attribute called ‘onclick’, you can input the function name. This tells the HTML to call the function ‘prompt_one()’ on the button click. When you click the button, a prompt will appear asking the user to enter the number.