Forum Moderators: open

Message Too Old, No Replies

Help with do/while loops

         

zero3ree

6:15 am on Nov 21, 2009 (gmt 0)

10+ Year Member



I am starting to create a program that uses various user prompts to create a list of employee name, number, deals and total amount of deals in dollars done.

I think I have the prompts correct. My problem is that my inner do/while loop. I am not sure how I can establish values for each iteration. Each one is to take a dollar amount and then add them together. Before i even get to the complicated function that I need to make I need to know how I separate those looped prompts into collectible values.

Plus does this do/while look correct because when I run it if the spNum is 0 it still runs.

You guys are the best by the way. I have learned so much from this site. Where do I donate...

zero3ree

6:17 am on Nov 21, 2009 (gmt 0)

10+ Year Member



Sorry


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 (dealNum<=sumDeal);
}

while (spNum!=0);

Fotiman

2:30 pm on Nov 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think you'll probably want to store the results in an array, where each array index represents an employee.

Also, in your example you will prompt for an employee name and how many deals they closed even if the user enters zero or negative because you're not checking that value until the end of the loop. Here's a reworked example that I think will do what you're trying to accomplish:


var employees = [];
// employees will be an array of objects that like this:
// {
// id: 1,
// name: "John Doe",
// deals: 3,
// total: 300
// }
var spNum = parseInt(prompt("Enter the next salesperson's employee number (zero or negative to exit)", "1"));
while (spNum > 0) {
var i = employees.length;
employees[i].id = spNum;
employees[i].name = prompt("What is employee #" + spNum + "'s name?");
employees[i].deals = parseInt(prompt("How many deals did " + employees[i].name + " close?"));
employees[i].total = 0;
var dealNum = 1;
if (employees[i].deals > 0) {
do {
employees[i].total += parseFloat(prompt("Dollar value of deal #" + dealNum + " for " + employees[i].name + "?"));
++dealNum;
} while (dealNum <= employees[i].deals);
}
spNum = parseInt(prompt("Enter the next salesperson's employee number (zero or negative to exit", "1"));
}

And if you'd like to donate, you can visit the Subscribeand Support WebmasterWorld [webmasterworld.com] page. :)

zero3ree

4:42 pm on Nov 23, 2009 (gmt 0)

10+ Year Member



So in this case I need to make an array with the employee name, deals and value of the deal already in the array. I was trying to set it up so as the user inputs the values they are collected into an array. And when the loop starts over all the values are output to the page. Maybe I'm wrong but this looks to have the values already saved and I pick from a list of employee numbers and the array has all the information already.

Fotiman

5:39 pm on Nov 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In the example I created, the array is created from the user input. You don't need to create the array with the employee details already in the array.
If you want to output the employee details when the loop starts over (technically, you would want to do it at the end of the loop, not the start), then you could simply output the values before the prompting for the employee number again:

// ...
// Everything above remains the same
// Output the employee details here:
alert("Employee ID: " + employees[i].id +
"\nEmployee Name: " + employees[i].name +
"\nDeals: " + employees[i].deals +
"\nTotal Value: " + employees[i].total);
spNum = parseInt(prompt("Enter the next salesperson's employee number (zero or negative to exit", "1"));
}

Just replace the alert with whatever code outputs to the page.
Alternatively, you could output to the page after you've collected all of the values by simply iterating through the employees array and outputing them the same way:

// ...
// Everything above remains the same
spNum = parseInt(prompt("Enter the next salesperson's employee number (zero or negative to exit", "1"));
}
for (var n = 0; n < employees.length; n++) {
// Output the employee details here:
alert("Employee ID: " + employees[n].id +
"\nEmployee Name: " + employees[n].name +
"\nDeals: " + employees[n].deals +
"\nTotal Value: " + employees[n].total);
}

Fotiman

5:47 pm on Nov 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Note, if you wanted to do the former (outputting the employee details immediately) and you have no need to reusing the results after outputting them, then instead of storing the values in an array, you could simply store the values in a single object. Like so:

var employee;
// employee will be an object like this:
// {
// id: 1,
// name: "John Doe",
// deals: 3,
// total: 300
// }
var spNum = parseInt(prompt("Enter the next salesperson's employee number (zero or negative to exit)", "1"));
while (spNum > 0) {
employee = {};
employee.id = spNum;
employee.name = prompt("What is employee #" + spNum + "'s name?");
employee.deals = parseInt(prompt("How many deals did " + employee.name + " close?"));
employee.total = 0;
var dealNum = 1;
if (employee.deals > 0) {
do {
employee.total += parseFloat(prompt("Dollar value of deal #" + dealNum + " for " + employee.name + "?"));
++dealNum;
} while (dealNum <= employee.deals);
}
alert("Employee ID: " + employee.id +
"\nEmployee Name: " + employee.name +
"\nDeals: " + employee.deals +
"\nTotal Value: " + employee.total);
spNum = parseInt(prompt("Enter the next salesperson's employee number (zero or negative to exit", "1"));
}

zero3ree

6:14 pm on Nov 23, 2009 (gmt 0)

10+ Year Member



That last way is perfect. Thank you. I want to post each one in a div or just a field set maybe. That part should be fine. So if I needed to work with each individual value for the deals how do I grab their value? Like say 3 deals and for each one depending on the value a certain percentage gets taken.

Fotiman

6:53 pm on Nov 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Do you mean that if an employee had 3 deals, rather than only the total accumulation for all 3 deals you want to have access to the details of each individual deal? If so, you'd need to modify the example to include an array of deals for each user:

var employee;
// employee will be an object like this:
// {
// id: 1,
// name: "John Doe",
// deals: [100,90,110],
// total: 300
// }
var spNum = parseInt(prompt("Enter the next salesperson's employee number (zero or negative to exit)", "1"));
while (spNum > 0) {
employee = {};
employee.id = spNum;
employee.name = prompt("What is employee #" + spNum + "'s name?");
employee.deals = [];
var deals = parseInt(prompt("How many deals did " + employee.name + " close?"));
employee.total = 0;
var dealNum = 1;
if (deals > 0) {
do {
employee.deals[employee.deals.length] = parseFloat(prompt("Dollar value of deal #" + dealNum + " for " + employee.name + "?"));
employee.total += employee.deals[employee.deals.length - 1];
++dealNum;
} while (dealNum <= deals);
}
alert("Employee ID: " + employee.id +
"\nEmployee Name: " + employee.name +
"\nDeals: " + employee.deals +
"\nTotal Value: " + employee.total);
spNum = parseInt(prompt("Enter the next salesperson's employee number (zero or negative to exit", "1"));
}

Note, I did not bother modifying the alert code to iterate through the employee.deals array, but it should be easy enough to do.
Hope that helps.

zero3ree

7:09 pm on Nov 23, 2009 (gmt 0)

10+ Year Member



You know maybe I don't need them separate. Just need to alter the total so that it equals 300 plus 8% of total. So i would say dealsTot = employee.total * .08 + 300. And then use that variable in the output?

Fotiman

7:28 pm on Nov 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, if you only need to modify the end total based on the sum of all deals, then you don't need to work with the individual deal numbers.

zero3ree

7:49 pm on Nov 23, 2009 (gmt 0)

10+ Year Member



Yeah altering the total is fine. One problem though that didn't work it just gave 300 as the answer. Seems to only recognize the 300 in my statement.

Fotiman

8:36 pm on Nov 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Can you post your code example? Perhaps a typo somewhere? If employee.total resolves to zero, then that would return 300.

zero3ree

8:40 pm on Nov 23, 2009 (gmt 0)

10+ Year Member



I figured it out and now it works like a charm. I put the dealsTot var above where it is evaluated. So it just used zero as the value for employee.total. Here is the finish program. Thank you for your help. By the way I can never get the\n to work. Does that not work in firefox or something?

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Sales</title>
</head>
<body>
<SCRIPT>
var employee;
var spNum = parseInt(prompt("Enter the next salesperson's employee number (zero or negative to exit)", "1"));
while (spNum > 0) {
employee = {}; // object to hold the values
employee.id = spNum;
employee.name = prompt("What is employee #" + spNum + "'s name?");
employee.deals = parseInt(prompt("How many deals did " + employee.name + " close?"));
employee.total = 0;
var dealNum = 1;
if (employee.deals > 0) {
do {
employee.total += parseFloat(prompt("Dollar value of deal #" + dealNum + " for " + employee.name + "?"));
++dealNum;
}// this loop controls the amount of values for deals
while (dealNum <= employee.deals);
}
dealsTot = employee.total;
totWage = 300 + dealsTot*0.08;// this will evaluate total sales to output salary.
document.write("<hr>Employee ID: " + employee.id +"<br>" );
document.write("Employee Name: " + employee.name +"<br>");
document.write("Deals: " + employee.deals +"<br>" );
document.write("Total Sales: " + employee.total.toFixed(2) +"<br>");
document.write("Total Salary: " + totWage +"<br>");
//outputs result and starts loop over
spNum = parseInt(prompt("Enter the next salesperson's employee number (zero or negative to exit", "1"));
}
</script>
</body>
</html>

Thanks this was a real challenge and I appreciate your help.

Fotiman

9:41 pm on Nov 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The "\n" will work if you are alerting the value, but not for document.write (alert takes a string, whereas document.write takes HTML).