Forum Moderators: open
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...
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);
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. :)
// ...
// 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"));
}
// ...
// 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);
}
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"));
}
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"));
}
<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.