943,865 Members | Top Members by Rank

Ad:
Oct 4th, 2008
0

Small JavaScript Error

Expand Post »
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.

/**
 * @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)
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  5. <title>Gross Pay Amount</title>
  6. <script type="text/javascript"
  7. language="Javascript"
  8. src ="grossPayAmount.js">
  9. </script>
  10. </head>
  11. <body>
  12.  
  13. <script type="text/javascript" language="JavaScript">
  14. var hoursWorked = prompt("Enter the number of hours worked:", "Enter hours worked here.");
  15. </script>
  16.  
  17. <script type="text/javascript" language="JavaScript">
  18. var hourlyRate = prompt("Enter hourly pay:", "Enter hourly pay here.");
  19. calculateGross (hourlyRate, hoursWorked);
  20. </script>
  21.  
  22.  
  23.  
  24. </body>
  25. </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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
jobojo is offline Offline
28 posts
since Oct 2008
Oct 4th, 2008
0

Re: Small JavaScript Error

Here you go!
Hope that this will solved the issue...

javascript Syntax (Toggle Plain Text)
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2.  
  3. <html>
  4.  
  5. <head>
  6.  
  7. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  8. <title>Gross Pay Amount</title>
  9. <script type="text/javascript">
  10. <!--
  11.  
  12. /* Assuming that this is an external srcript */
  13.  
  14. function thisPrompt()
  15. {
  16. var nVal = /^[0-9]{1,4}[\.\d]{0,2}$/;
  17. do {
  18.  
  19. var h = 'Enter the hours of worked';
  20. var ht = 'Hours of worked';
  21. var hrpy = 'Enter hourly pay here';
  22. var hrpyT = 'Hourly Pay';
  23. var hours = prompt(h,ht);
  24.  
  25. while(!nVal.test(hours)) {
  26. alert('\nOnly numeric values is allowed on this input!'); hours = prompt(h, ht);
  27. }
  28.  
  29. var rate = prompt(hrpy, hrpyT);
  30. while(!nVal.test(rate)) {
  31.  
  32. alert('\nOnly numeric values is allowed on this input!');
  33. rate = prompt(hrpy,hrpyT);
  34. }
  35.  
  36. } while(!nVal.test(hours && rate));
  37. calculateGross(hours,rate);
  38. }
  39.  
  40. if (window.addEventListener)
  41. window.addEventListener('load', thisPrompt, false);
  42. else if(window.attachEvent)
  43. window.attachEvent('onload', thisPrompt);
  44. else
  45. window.onload = thisPrompt;
  46.  
  47. function calculateGross(hours,rate)
  48. {
  49. var pay = parseFloat((rate * hours), 2);
  50. var overTime = parseFloat((((hours - 40) * (1.5 * rate)) + (rate * 40)), 2);
  51.  
  52. var results = ( hours <= 40 ) ? pay : overTime;
  53.  
  54. /* Just to check if the results
  55.   is getting right values' of
  56.   the prompt boxes! */
  57.  
  58. document.getElementById('total').innerHTML = 'Total Pay: $' + results;
  59. }
  60.  
  61. // -->
  62. </script>
  63. </head>
  64. <body>
  65. <div id="total" style="width: 90px; text-align: center; font-size: 12px; background: #CC0000; color: #FFF; border: 2px solid #CCC">&nbsp;</div>
  66.  
  67. </body>
  68. </html>
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008
Oct 4th, 2008
0

Re: Small JavaScript Error

Here you go!
Hope that this will solved the issue...

javascript Syntax (Toggle Plain Text)
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2.  
  3. <html>
  4.  
  5. <head>
  6.  
  7. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  8. <title>Gross Pay Amount</title>
  9. <script type="text/javascript">
  10. <!--
  11.  
  12. /* Assuming that this is an external srcript */
  13.  
  14. function thisPrompt()
  15. {
  16. var nVal = /^[0-9]{1,4}[\.\d]{0,2}$/;
  17. do {
  18.  
  19. var h = 'Enter the hours of worked';
  20. var ht = 'Hours of worked';
  21. var hrpy = 'Enter hourly pay here';
  22. var hrpyT = 'Hourly Pay';
  23. var hours = prompt(h,ht);
  24.  
  25. while(!nVal.test(hours)) {
  26. alert('\nOnly numeric values is allowed on this input!'); hours = prompt(h, ht);
  27. }
  28.  
  29. var rate = prompt(hrpy, hrpyT);
  30. while(!nVal.test(rate)) {
  31.  
  32. alert('\nOnly numeric values is allowed on this input!');
  33. rate = prompt(hrpy,hrpyT);
  34. }
  35.  
  36. } while(!nVal.test(hours && rate));
  37. calculateGross(hours,rate);
  38. }
  39.  
  40. if (window.addEventListener)
  41. window.addEventListener('load', thisPrompt, false);
  42. else if(window.attachEvent)
  43. window.attachEvent('onload', thisPrompt);
  44. else
  45. window.onload = thisPrompt;
  46.  
  47. function calculateGross(hours,rate)
  48. {
  49. var pay = parseFloat((rate * hours), 2);
  50. var overTime = parseFloat((((hours - 40) * (1.5 * rate)) + (rate * 40)), 2);
  51.  
  52. var results = ( hours <= 40 ) ? pay : overTime;
  53.  
  54. /* Just to check if the results
  55.   is getting right values' of
  56.   the prompt boxes! */
  57.  
  58. document.getElementById('total').innerHTML = 'Total Pay: $' + results;
  59. }
  60.  
  61. // -->
  62. </script>
  63. </head>
  64. <body>
  65. <div id="total" style="width: 90px; text-align: center; font-size: 12px; background: #CC0000; color: #FFF; border: 2px solid #CCC">&nbsp;</div>
  66.  
  67. </body>
  68. </html>
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008
Oct 4th, 2008
0

Re: Small JavaScript Error

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.

Click to Expand / Collapse  Quote originally posted by essential ...
Here you go!
Hope that this will solved the issue...

[code=javascript]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
Reputation Points: 10
Solved Threads: 0
Light Poster
jobojo is offline Offline
28 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: javascript injection
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Javascript Not Working in IE7





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC