JavaScript Course

Lesson 6: Stopping a Program to get data

Have you ever been asked a question by the computer when working on it? Why do you think you were asked the question? What did the computer do with your answer? Think about it. That's what this lesson is about.

Most times when writing a program the programmer (that's you by the way) has no idea what value the variables in the program will take. In these situations the programmer will often want to stop the program so that the person running the program can type in some data. Consider the following program carefully. You should know what the first 4 instructions will do. Watch what happens when you come to the 5th. Can you predict the output of the 6th instruction?

x=20
alert ( " x is starting at " + x )
x=30
alert ( " x is now " + x )
x=promptNumber("Enter a value for x in the box below!")
alert ( "And finally x is " + x)

You will probably guess what the output from the first 2 alert instructions will be, but what do you think the output from the 3rd alert instruction will be? Why not paste the program in the box below and execute it a few times to see if you can work out what the 3rd assignment instruction (x=prompt....) is doing? (If you feel you need to, refer to the lecture notes)


Any idea yet? If it worked properly, the 3rd assignment instruction stopped the computer and put a message up on the screen. This is very like the effect of an alert instruction, however, this window has a small area that you can type something into. If you typed a number into this box before clicking on OK, then this number was stored in the variable x. (If you do not enter anumber before clicking on OK, zero will be stored in x. If you type some text in the box, then NaN, which means 'Not a Number', will be stored in the variable x).

Exercise

Change the message in the 3rd assignment instruction above (the one with the promptNumber in it) so that it says something like: "Please type in a number!" and see if it works. If it does it should show you that you can have any message you like when you stop a program to get the 'user' to type in some data. Make a note of the old and the new versions of the instruction in your workbook only.

The promptNumber function

The item on the right hand side of the equal sign in the assignment instruction ( x =... which means: store in the variable x) in the above program is called a promptNumber function. Now, take a note in your workbook of exactly what it does!

The promptNumber function does 4 things:

  1. It stops the computer program running.
  2. It outputs a message to give you an idea of what data it requires (that's why it is called a prompt function)
  3. It allows you to type in data in a box.
  4. It makes the data typed into the box available for use by other operations (e.g. to be stored in a variable in an assignment instruction.)

Assignment 6

Write a JavaScript program in the text area below to do the following :

  1. Print out your name.
  2. Stop the computer and ask the user to type in the cost of an item.
  3. Provide a text area for the user to type in a value.
  4. Store the number entered in a variable called cost.
  5. Output the cost of the item as follows: The item you purchased cost (whatever it is) pounds.

Hint: 2, 3 and 4 are all done with the one instruction. Which one?


Execute the program several times. Each time the program is run it should accurately tell you the cost that you have just typed in.

Make a note of the program in the above box.

End of Lesson : To go back to where you came from click on Back on the toolbar.