PDA

View Full Version : Help with do/while



zero3ree
Nov 20th, 2009, 06:40 PM
part 1: I am trying to set up some prompts. The prompts need to gather user input. One of the prompts a quantity and the next prompt loops to the amount entered in the previous. I tried to add a inner do/while and Im having some problems. The loop won't terminate.

Part 2: How do i make those looping prompts there own individual values? As in i set a variable to the prompt but I don't know how each looping prompt has its own value.

here is my prompts in a do/while



do {var spNum=parseInt(prompt("Enter the next salesperson's employee number (zero or negative to exit", "1"));
var empName = prompt("Enter employee #"+spNum+ " what is their name?");
var sumDeal = parseInt(prompt("How many deals did" +empName+ "close?"));
var dealNum=1;
do {var dealTot = parseFloat(prompt("Dollar value of deal #" +dealNum+ "for" +empName+ ""));
++dealNum;
}
while (sumDeal>0);
}
while (spNum>0);


dealTot is the prompt that needs to loop and the amount of loops is set from sumDeal prompt.

Any help is appreciated.

gusblake
Nov 20th, 2009, 06:52 PM
The loop never terminates because sumDeal is always >0 (you have to change it from within the inner loop - it can't be done from the outer loop because the outer loop stops until the inner loop is finished.)

As for the individual values, you could use an array and increment a pointer variable for each cycle of the loop.

zero3ree
Nov 21st, 2009, 06:11 AM
Ok that was a stupid error on my part. i changed it to dealNum<=sumDeal and now it works. But im not understanding the part 2 of your explanation. I don't know what you mean by pointer var.

gusblake
Nov 21st, 2009, 12:35 PM
You would define sumDeal as an empty array before the loop (var sumDeal=[]; ) then instead of


sumDeal=prompt()

You would write


sumDeal.push(prompt()) //a pointer variable isn't necessary, not sure what I was thinking there

Which appends the return value of the prompt to the sumDeal array.

Or even better, define it as an empty object and use empName as the key:


sumDeal[empName]=prompt()

Then you could access it with alert(sumDeal.john)

zero3ree
Nov 22nd, 2009, 06:58 PM
So I make sumDeal the array? I need the values inputted prom the dealTot var to be collected.

gusblake
Nov 22nd, 2009, 07:32 PM
dealTot can be done just as easily...



var dealTot={}; //empty object

do {var spNum=parseInt(prompt("Enter the next salesperson's employee number (zero or negative to exit", "1"));
var empName = prompt("Enter employee #"+spNum+ " what is their name?");
var sumDeal = parseInt(prompt("How many deals did" +empName+ "close?"));
var dealNum=1;
do {
dealTot[empName] = parseFloat(prompt("Dollar value of deal #" +dealNum+ "for" +empName+ ""));
++dealNum;
}
while (sumDeal>0);
}
while (spNum>0);

alert(dealTot.name) //alerts the deal total associated with name, assuming "name" was entered in one of the prompts


Edit - forgot to take out var keyword on line 8

gusblake
Nov 22nd, 2009, 08:01 PM
Also you can insert


if(!spNum) break;

after the first prompt, which stops it looping one more time after you enter the zero-or-negative value.

zero3ree
Nov 23rd, 2009, 03:39 AM
Ok this is what I have and I can't get the alert to work. Do I need to do this for each prompt and then have a document write with all the array names in it?



var dealTot = {};
do {var spNum=parseInt(prompt("Enter the next salesperson's employee number (zero or negative to exit", "1"));
if(!spNum) break;
var empName = prompt("Enter employee #"+spNum+ " what is their name?");
var sumDeal = parseInt(prompt("How many deals did" +empName+ "close?"));
var dealNum=1;
do {
dealTot[empName] = parseFloat(prompt("Dollar value of deal #" +dealNum+ "for" +empName+ ""));
++dealNum;
}
while (dealNum<=sumDeal);
}
while (spNum>0);
alert(dealTot.name)