...

View Full Version : Need Help Small Errorr



shaz786
06-13-2007, 06:31 PM
HEARS THE CODE BELOW THE PROBLEM IS

Hours worked: 62.00 ( SHOULD DISPLAY HOURS HEAR )
Gross Pay: N.aN ( IT SHOULD HAVE PAY HEAR)
IF U GUY CAN HELP EMAIL ME THE FILE BACK TO
SHAZYPC@HOTMAIL.COM



<html>
<head>
<title>Payslip Program</title>

<script>

// ***************************************
// Name: Shahzad 786
// Date: 31st May 2007
// Purpose: A script to produce payslips
// for a small business
// ***************************************


function setUpArrays()
// Sets up the arrays with names, employee numbers and national insurance numbers
{
names[1] = "Jay Khan"
names[2] = "Shaz A"
names[3] = "Tom Anderson"
names[4] = "Jhon Khan"
names[5] = "Iram Khan"
names[6] = "Sean D"
names[7] = "Jhon S"
names[8] = "ALex"
names[9] = "Craig Lindsay"
names[10] = "George Anderson"

EmplNo[1] = "101"
EmplNo[2] = "102"
EmplNo[3] = "103"
EmplNo[4] = "104"
EmplNo[5] = "105"
EmplNo[6] = "106"
EmplNo[7] = "107"
EmplNo[8] = "108"
EmplNo[9] = "109"
EmplNo[10] = "110"

NatIns[1] = "A/786"
NatIns[2] = "B/112"
NatIns[3] = "C/654"
NatIns[4] = "D/147"
NatIns[5] = "E/852"
NatIns[6] = "F/963"
NatIns[7] = "G/789"
NatIns[8] = "H/127"
NatIns[9] = "I/778"
NatIns[10] = "K/775"

}

function getEmployeesData()
// Gets the date and the number of Hours worked by each employee
{
maxHours = 50

date = prompt("Enter today's date: ","")

for (i=1; i<=maxEmployees; i++)
{
Hours[i] = prompt("Enter number of Hours worked by " + names[i] + ": ","0")
while (Hours[i] > maxHours)
{
alert("No employee is allowed to work more than " + maxHours + " Hours")
Hours[i] = prompt("Enter number of Hours worked by " + names[i] + ": ","0")
}
}

}


function showreal (x, d)
// Displays a real number (x) with d decimal places
{
// Multiply by 100, round the answer, and convert it to a string
s = Math.round(x*100).toString()

// Now insert a decimal point before the dth decimal place
return s.substring(0,s.length-d) + "." + s.substring(s.length-d,s.length)
}


function calculatePay()
// Calculate the number of hours worked by each Employee
{
MaxHours = 50
MinPay = 6.20
MaxPay = 9.30

for (i=1; i<=maxEmployees; i++)
{
if (Hours[i] < MaxHours)
{
Pay[i] = Hours[i] * MinPay
}
else
{
pay[i] = MinHours * MinPay + (Hours[i] - MaxHours) * MaxPay
}
}
}



function displayPayslips()
// Calculates and displays the results
{
for (i=1; i<=maxEmployees; i++)
{
document.write ("<h1>Payslip Program</h1><br><br>")
document.write("<h2>Payslip For " + date + "</h2>")
document.write("<table border='2' width='50%' cellspacing='2' cellpadding='2'>")
document.write("<tr><td width='60%'><b>Name:</b></td>")
document.write("<td width='40%'>" + names[i] + "</td></tr>")
document.write("<tr><td><b>Employee Number:</b></td>")
document.write("<td>" + EmplNo[i] + "</td></tr>")
document.write("<tr><td><b>National Insurance Number:</b></td>")
document.write("<td>" + NatIns[i] + "</td></tr>")
document.write("<tr><td><b>Hours worked:</b></td>")
document.write("<td>" + showreal(Pay[i],2) + "</td></tr>")
document.write("<tr><td><b>Gross Pay:</b></td>")


document.write("<td>" + showreal(Pay,2) + "</td></tr>")
document.write("</table>")
document.write("<br><hr><br>")
}
}


</script>

</head>

<body bgcolor="lightyellow">

<script>
maxEmployees = 10
var date

names = new Array()
EmplNo = new Array()
NatIns = new Array()
Hours = new Array()
Pay = new Array()

//Main block of code
setUpArrays()
getEmployeesData()
calculatePay()
displayPayslips()

</script>

</body>

</html>

Philip M
06-13-2007, 07:13 PM
Three errors:-

pay[i] = MinHours * MinPay + (Hours[i] - MaxHours) * MaxPay
should be

Pay[i] = MinHours * MinPay + (Hours[i] - MaxHours) * MaxPay
(JavaScript is case sensitive)

document.write("<td>" + showreal(Pay,2) + "</td></tr>")
should be
document.write("<td>" + showreal(Pay[i],2) + "</td></tr>")

document.write("<td>" + showreal(Pay[i],2) + "</td></tr>")
should be
document.write("<td>" + showreal(Hours[i],2) + "</td></tr>")



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum