| | |
Small JavaScript Error
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Oct 2008
Posts: 19
Reputation:
Solved Threads: 0
Good evening everyone. I am new here and also new to JavaScript. I have a small assignment that I think I have just about done, but the False part of my conditional expression is not calculating properly. My program keeps 'spitting out' the same calculation of rate times hours if the hours worked are below 40 and if the hours worked are above 40 instead of doing different calculation for both. My conditional expression (?) is calculating the true part correct, but not the false part. Below is my .js file that I have.
Here is my .html file with the appropriate prompts:
Does anybody see my error? Is it in my overTime formula or do I have something else wrong? Any help would be sincerely appreciated. Thanks in advance and have a great weekend everyone.
/**
* @author Administrator
*/
//If hours worked are less than 40,
//your regular pay rate will be used.
//Otherwise employee will be paid one and a half times their regular pay rate,
//for hours worked over 40.
function calculateGross(hours, rate)
{
var pay = rate * hours;
pay = parseFloat (pay, 2);
var overTime = ((hours - 40) * (1.5 * rate)) + (rate * 40);
overTime = parseFloat (overTime, 2)
var result;
result = parseFloat(result, 2);
hours <= 40 ? result = pay : result = overTime;
document.writeln(result);
}Here is my .html file with the appropriate prompts:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Gross Pay Amount</title> <script type="text/javascript" language="Javascript" src ="grossPayAmount.js"> </script> </head> <body> <script type="text/javascript" language="JavaScript"> var hoursWorked = prompt("Enter the number of hours worked:", "Enter hours worked here."); </script> <script type="text/javascript" language="JavaScript"> var hourlyRate = prompt("Enter hourly pay:", "Enter hourly pay here."); calculateGross (hourlyRate, hoursWorked); </script> </body> </html>
Does anybody see my error? Is it in my overTime formula or do I have something else wrong? Any help would be sincerely appreciated. Thanks in advance and have a great weekend everyone.
Here you go!
Hope that this will solved the issue...
Hope that this will solved the issue...
javascript Syntax (Toggle Plain Text)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Gross Pay Amount</title> <script type="text/javascript"> <!-- /* Assuming that this is an external srcript */ function thisPrompt() { var nVal = /^[0-9]{1,4}[\.\d]{0,2}$/; do { var h = 'Enter the hours of worked'; var ht = 'Hours of worked'; var hrpy = 'Enter hourly pay here'; var hrpyT = 'Hourly Pay'; var hours = prompt(h,ht); while(!nVal.test(hours)) { alert('\nOnly numeric values is allowed on this input!'); hours = prompt(h, ht); } var rate = prompt(hrpy, hrpyT); while(!nVal.test(rate)) { alert('\nOnly numeric values is allowed on this input!'); rate = prompt(hrpy,hrpyT); } } while(!nVal.test(hours && rate)); calculateGross(hours,rate); } if (window.addEventListener) window.addEventListener('load', thisPrompt, false); else if(window.attachEvent) window.attachEvent('onload', thisPrompt); else window.onload = thisPrompt; function calculateGross(hours,rate) { var pay = parseFloat((rate * hours), 2); var overTime = parseFloat((((hours - 40) * (1.5 * rate)) + (rate * 40)), 2); var results = ( hours <= 40 ) ? pay : overTime; /* Just to check if the results is getting right values' of the prompt boxes! */ document.getElementById('total').innerHTML = 'Total Pay: $' + results; } // --> </script> </head> <body> <div id="total" style="width: 90px; text-align: center; font-size: 12px; background: #CC0000; color: #FFF; border: 2px solid #CCC"> </div> </body> </html>
Here you go!
Hope that this will solved the issue...
Hope that this will solved the issue...
javascript Syntax (Toggle Plain Text)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Gross Pay Amount</title> <script type="text/javascript"> <!-- /* Assuming that this is an external srcript */ function thisPrompt() { var nVal = /^[0-9]{1,4}[\.\d]{0,2}$/; do { var h = 'Enter the hours of worked'; var ht = 'Hours of worked'; var hrpy = 'Enter hourly pay here'; var hrpyT = 'Hourly Pay'; var hours = prompt(h,ht); while(!nVal.test(hours)) { alert('\nOnly numeric values is allowed on this input!'); hours = prompt(h, ht); } var rate = prompt(hrpy, hrpyT); while(!nVal.test(rate)) { alert('\nOnly numeric values is allowed on this input!'); rate = prompt(hrpy,hrpyT); } } while(!nVal.test(hours && rate)); calculateGross(hours,rate); } if (window.addEventListener) window.addEventListener('load', thisPrompt, false); else if(window.attachEvent) window.attachEvent('onload', thisPrompt); else window.onload = thisPrompt; function calculateGross(hours,rate) { var pay = parseFloat((rate * hours), 2); var overTime = parseFloat((((hours - 40) * (1.5 * rate)) + (rate * 40)), 2); var results = ( hours <= 40 ) ? pay : overTime; /* Just to check if the results is getting right values' of the prompt boxes! */ document.getElementById('total').innerHTML = 'Total Pay: $' + results; } // --> </script> </head> <body> <div id="total" style="width: 90px; text-align: center; font-size: 12px; background: #CC0000; color: #FFF; border: 2px solid #CCC"> </div> </body> </html>
•
•
Join Date: Oct 2008
Posts: 19
Reputation:
Solved Threads: 0
Thank you for your time and assistance. Reviewing your information and comparing a few things helped me to catch my problem. The problem has been resolved and I appreciate your help I did use a couple of shortcuts within your code to shorten a couple of my variable lines up a tiny bit (adding parsefloat to my variable line itself). Thanks again and have a great weekend.
![]() |
Similar Threads
- Javascript Firefox Help (JavaScript / DHTML / AJAX)
- small error when submitting form (PHP)
- Javascript Login with Redirection Issues (JavaScript / DHTML / AJAX)
- Help Needed Parse error: syntax error (PHP)
- xhtml-javascript taxtarea problem (JavaScript / DHTML / AJAX)
- error on page!! Cannot solve!!Help (ASP.NET)
- VB Script Error, Shaky IE, Endless Popups (Viruses, Spyware and other Nasties)
- help with javascript (moved from javascript forum) (PHP)
- a weird Javascript error (JavaScript / DHTML / AJAX)
- I need help with a parse error! (PHP)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: javascript injection
- Next Thread: Javascript Not Working in IE7
| Thread Tools | Search this Thread |
acid2 ajax ajaxcode ajaxhelp animate array automatically beta box bug calendar cart checkbox class codes column cookies createrange() css cursor date debugger decimal design dom download dropdown editor element enter error events explorer firefox focus form frameworks getselection google gwt html htmlform ie8 iframe image() images index internet java javascript jawascriptruntimeerror jquery jsf jsfile jsp jump listbox maps masterpage math menu microsoft mimic mp4 object onmouseover onreadystatechange parent php player post problem programming progressbar prototype redirect regex runtime safari scale scriptlets search select session shopping size sql text textarea toggle validation variables w3c web website window windowofwords windowsxp wysiwyg \n





