JavaScript Prompt() method

I have an assignment I'm working on and I am having a problem with the prompt() method. I see that I can do one prompt, but I need several and with amounts.

For Example...

I have created an HTML table with many artists and columns with DVD's, CD's and Audio. All with prices in their rows. I need to write a prompt that will do this.

Using the prompt() method, ask the user to enter the artist's name, the number of DVD's, the number of CD's and the number of audio cassette's the user wishes to purchase. Save the answers in seperate variables. Also use a seperate prompt for each value. Any advice would be so appreciated!

Edit: code from comment below:

var w=window.prompt("please enter your name");
window.alert(w);
var x=widow.prompt ("Enter how many DVDs you want to buy");
window.alert(x);
var y=window.alert ("Enter how many CDs you want to buy");
window.alert(y);
var z=window.alert ("Enter how many Audio Cassettes you want to buy");
window.alert(z);

Upvotes: 3

Views: 12497

Answers (4)

Sean O'Connor
Sean O'Connor

Reputation: 1

prompt is pretty easy, this is how I use it:

var value = prompt( message );

Upvotes: 0

Sampson
Sampson

Reputation: 268324

From the sounds of it, the following meets your requirements:

var a, d, t;

while ( ! a ) a = prompt( "Which Artist?" );
while ( ! d ) d = prompt( "How many DVDs?" );
while ( ! t ) t = prompt( "How many tapes?" );

alert( "You want " + t + " Tapes, and " + d + " DVDs, of " + a + "." );

Let's break it down you so have an understanding of what's going on:

var a, d, t;

On the first line, I'm declaring the various variables I plan on using in the code below. This is a common practice, and would be a good habit to develop if you want to maintain clean and manageable code.

while ( ! a )

The while loop is a loop that will run over and over, until a condition is met. In this example, we're telling the loop to run as long as we don't have a value for a. What comes next is our attempt to collect a value of a from the user:

while ( ! a ) a = prompt( "Which Artist?" );

Each time the while loop runs, we will prompt the user to answer the question. We take their answer, and assign it to a. If they entered nothing, our while loop runs again, prompting them again. You can probably make sense of the next two while loops at this point.

Lastly is our alert, which gathers up the various values and shows them to the user:

alert( 'Artist ' + a );

This also presents an example of string concatenation, or joining together of two strings. We have a value stored inside a, and a value written explicitly as text. We use the + operator to join both of these together, like glue tying two ends of a rope together. As we add more strings, and more variables, we use the + operator more and more:

alert( "You want " + t + " Tapes, and " + d + " DVDs, of " + a + "." );

When this code is ran, t, d, and a will all be replaced with the actual values inserted by the end-user.

Note, this is a very basic implementation of what your homework requires. A real solution would test the type of input to make sure it's of the expected format. For instance, when asking how many DVDs the user wants, you may want to restrict 'acceptable' answers to integers only.

Best of luck!

Upvotes: 5

SGK
SGK

Reputation: 81

Use multiple functions such that on click of the first prompt the other function gets called which handles another prompt where u can save the variable value seperately.use it recursively.

Upvotes: 2

Bergi
Bergi

Reputation: 664206

Use a loop over the values object/array. Maybe use a second (nested) loop to prompt again until a value was entered.

Upvotes: 2

Related Questions