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: jobojo is an unknown quantity at this point 
Solved Threads: 0
jobojo jobojo is offline Offline
Newbie Poster

Small JavaScript Error

 
0
  #1
Oct 4th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: Small JavaScript Error

 
0
  #2
Oct 4th, 2008
Here you go!
Hope that this will solved the issue...

  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>
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: Small JavaScript Error

 
0
  #3
Oct 4th, 2008
Here you go!
Hope that this will solved the issue...

  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>
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 19
Reputation: jobojo is an unknown quantity at this point 
Solved Threads: 0
jobojo jobojo is offline Offline
Newbie Poster

Re: Small JavaScript Error

 
0
  #4
Oct 4th, 2008
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.

Originally Posted by essential View Post
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">
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC