fab5freddy Posted November 13, 2010 Report Posted November 13, 2010 Recently in my javascript class we started working a little bit with jquery and getting information from databases. I have the very very basics down such as retrieving the records. However I missed a few days of class and now am unsure how to find the totals, averages and other basic math functions. In the program I am writing I have to display the item number, description and stock amount. I have that just find but can not find anywhere that tells me how to find the total and the average. I have a few variables but everything I've tried doesn't work. I thought it might be something with SQL but not sure. Here is my code so far: function lab05recordSetProcessing() { // Declare Variables var currentNumber; var currentDescription; var currentStockAmount; var totalInventory; var averageInventoryItems; var itemRecords; // Open the Inventory Items Records and make them // available to the script itemRecords = openInventoryItemsRecords(); document.write("The Inventory Items Record Set:<br /><br />"); // Read the next record, test to see if there // is a next record and then output it while (itemRecords.readNextRecord()) { currentNumber = itemRecords.getItemNumber(); currentDescription = itemRecords.getItemDescription(); currentStockAmount = itemRecords.getItemStockAmount(); document.write(currentNumber + "\t" + currentDescription + "\t" + currentStockAmount + "<br />"); } } Quote
falkencreative Posted November 14, 2010 Report Posted November 14, 2010 I believe getting totals/averages would be some basic math you'd need to do within the Javascript, and isn't SQL related at all. For example, to get total inventory you would need to: - define a variable (it looks like you've done that with "var totalInventory;") - set that variable to 0 initially - within your while loop, add to that variable every time it loops (something like "totalInventory = totalInventory + itemRecords.getItemStockAmount"?) - then write out the results once outside of the loop. Once you've figured out the total inventory, I believe you could use that to find the average (which I believe would be total number of unique items divided by total number of items in inventory?) Quote
fab5freddy Posted November 14, 2010 Author Report Posted November 14, 2010 Thanks so much for the help things worked great. I've now run into another problem. I have to use a database to find gross amount of pay, withholding amount, net pay, employee number, and employee name. Once the whole program is run I have to find the total of the gross pay the only problem is there is two separate gross amount one for regular pay if hours are 35 or below and than overtime pay for hours over 35. I have everything working fine I just can't figure out how to get the total for all gross pay. Here is my code let me know if there is any clarification that is needed. I'm sorry it may look messy I am very new to this and appreciate any help at all. /* This is the JavaScript code for "Employee Payroll" File: /unit4/project/project4Part2.html */ function project4Part2() { // Your code goes in here. //Constant Variables var ZERO = 0; var THIRTY_FIVE = 35; var ONE_AND_HALF = 1.5; var FIFTEEN_PERCENT = .15 //Variables var employeeNumber; var employeeName; var employeeWage; var employeeHours; var regularPay; var grossPay; var overTimePay; var overTimeHours; var withHoldingAmount; var noOverTimePay; var noOverTimeGrossPay; var noOverTimeWithHoldingAmount; var noOverTimeNetPay; var netPay; var counter; //Open Payroll Records var employeeInformation = openEmployeePayrollRecords(); counter = ZERO; //Create table to display results document.write("Employee\t" + "Employee\t" + "Gross\t" + "Withholding\t" + "Net\n"); document.write("Number\t\t" + "Name\t\t" + "Pay\t" + "Amount\t\t" + "Pay" + "\n\n"); //Look at each record and display information while (employeeInformation.readNextRecord()) { employeeNumber = employeeInformation.getEmployeeNumber(); employeeName = employeeInformation.getEmployeeName(); employeeWage = employeeInformation.getEmployeeHourlyWage(); employeeHours = employeeInformation.getEmployeeHoursWorked(); //Calculations noOverTimeGrossPay = employeeWage * employeeHours; regularPay = employeeWage * THIRTY_FIVE; overTimeHours = employeeHours - THIRTY_FIVE; overTimePay = overTimeHours * (employeeWage * ONE_AND_HALF); grossPay = regularPay + overTimePay; noOverTimeWithHoldingAmount = noOverTimeGrossPay * FIFTEEN_PERCENT; withHoldingAmount = grossPay * FIFTEEN_PERCENT; netPay = grossPay - withHoldingAmount; noOverTimeNetPay = noOverTimeGrossPay - noOverTimeWithHoldingAmount; if (employeeHours > 35) { document.write(employeeNumber + "\t\t" + employeeName + "\t" + grossPay + "\t" + withHoldingAmount + "\t\t" + netPay + "\n"); } else { document.write(employeeNumber + "\t\t" + employeeName + "\t" + noOverTimeGrossPay + "\t" + noOverTimeWithHoldingAmount + "\t\t" + noOverTimeNetPay + "\n"); } } } Quote
falkencreative Posted November 14, 2010 Report Posted November 14, 2010 Wouldn't you need to create a totalGrossPay variable, set it initially to 0, and every time you loop, add both regular pay and overtime pay to it? Quote
fab5freddy Posted November 14, 2010 Author Report Posted November 14, 2010 I still can't get it to work. The problem is I have to different totals under gross pay. One for people who worked over 35 with over time pay and another one for people who worked 35 hours and under with no over time pay. Here is the exact problem description: "Design a program that will process the weekly employee time cards for all the employees of an organization. Each employee time card will have the these data items: an employee number, an employee name, an hourly wage rate, and the number hours worked during the week. Each employee is to be paid time-and-a-half for all hours worked over 35. A tax amount of 15% of gross salary is to be withheld. The output to the screen should display the employee's number and name, gross pay, withholding amount, and net pay. At the end of the report, display the total payroll amount and the average amount paid per employee." Everything is great it's the total part at the end of the report I can't get. Here is my code so far: /* This is the JavaScript code for "Employee Payroll" File: /unit4/project/project4Part2.html */ function project4Part2() { // Your code goes in here. //Constant Variables var ZERO = 0; var THIRTY_FIVE = 35; var ONE_AND_HALF = 1.5; var FIFTEEN_PERCENT = .15 //Variables var employeeNumber; var employeeName; var employeeWage; var employeeHours; var regularPay; var grossPay; var overTimePay; var overTimeHours; var withHoldingAmount; var noOverTimePay; var noOverTimeGrossPay; var noOverTimeWithHoldingAmount; var noOverTimeNetPay; var netPay; var totalPay; totalPay = ZERO; //Open Payroll Records var employeeInformation = openEmployeePayrollRecords(); //Create table to display results document.write("Employee\t" + "Employee\t" + "Gross\t" + "Withholding\t" + "Net\n"); document.write("Number\t\t" + "Name\t\t" + "Pay\t" + "Amount\t\t" + "Pay" + "\n\n"); //Look at each record and display information while (employeeInformation.readNextRecord()) { employeeNumber = employeeInformation.getEmployeeNumber(); employeeName = employeeInformation.getEmployeeName(); employeeWage = employeeInformation.getEmployeeHourlyWage(); employeeHours = employeeInformation.getEmployeeHoursWorked(); //Calculations regularPay = employeeWage * THIRTY_FIVE; overTimeHours = employeeHours - THIRTY_FIVE; overTimePay = overTimeHours * (employeeWage * ONE_AND_HALF); grossPay = regularPay + overTimePay; noOverTimeGrossPay = employeeWage * employeeHours; noOverTimeWithHoldingAmount = noOverTimeGrossPay * FIFTEEN_PERCENT; withHoldingAmount = grossPay * FIFTEEN_PERCENT; netPay = grossPay - withHoldingAmount; noOverTimeNetPay = noOverTimeGrossPay - noOverTimeWithHoldingAmount; totalPay = totalPay + noOverTimeGrossPay + overTimePay; if (employeeHours > 35) { document.write(employeeNumber + "\t\t" + employeeName + "\t" + grossPay + "\t" + withHoldingAmount + "\t\t" + netPay + "\n"); } else { document.write(employeeNumber + "\t\t" + employeeName + "\t" + noOverTimeGrossPay + "\t" + noOverTimeWithHoldingAmount + "\t\t" + noOverTimeNetPay + "\n"); } } document.write(totalPay); } Quote
falkencreative Posted November 15, 2010 Report Posted November 15, 2010 What should "totalPay" have at the end of the code? What are you currently getting? Shouldn't this line: totalPay = totalPay + noOverTimeGrossPay + overTimePay; be totalPay = totalPay + grossPay; ? Quote
fab5freddy Posted November 15, 2010 Author Report Posted November 15, 2010 When I use the code totalPay = totalPay + grossPay; I get 31428.75 the answer I need to come up with is 31796.25. Using the code I provided here is the chart with results. I need the numbers listed under Gross Pay to equal 31796.25. I had to attach the chart since I could figure out any other way to add it. If there is an easier way let me know. Chart.rtf Quote
falkencreative Posted November 15, 2010 Report Posted November 15, 2010 I'm not really sure where the issue is, but I unfortunately don't have time to look at this in depth. However, I do know that your total pay should be the result of the gross pay of all employees. There must be some bad math somewhere that is causing the issue. Are all of your other results correct? Quote
fab5freddy Posted November 15, 2010 Author Report Posted November 15, 2010 All the other results are correct. I'm gonna just take a little rest from it and look at it tomorrow more which usually helps. I really appreciate all the help you have given me. You have been a great help. I was wondering if you knew any good books or websites for a beginner the book my instructor has us using doesn't follow along with the course work at all and is very frustrating. He just tells us to go out on are own and find books. However for a beginner and can be a little overwhelming trying to find a decent book to start with. Once again thanks for your help. Quote
fab5freddy Posted November 15, 2010 Author Report Posted November 15, 2010 I figured it out I had to do the math in the if statement to get the results I wanted. It was what you said about going back and looking at the math that made me remember an issue I ran into early. Thanks again. Quote
falkencreative Posted November 15, 2010 Report Posted November 15, 2010 Awesome. Glad you got it figured out. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.