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:

<!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.

Recommended Answers

All 3 Replies

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

<!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: &#36;' + results;
}

// -->
</script>
</head> 
<body> 
<div id="total" style="width: 90px; text-align: center; font-size: 12px; background: #CC0000; color: #FFF; border: 2px solid #CCC">&nbsp;</div>

</body> 
</html>

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

<!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: &#36;' + results;
}

// -->
</script>
</head> 
<body> 
<div id="total" style="width: 90px; text-align: center; font-size: 12px; background: #CC0000; color: #FFF; border: 2px solid #CCC">&nbsp;</div>

</body> 
</html>

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.

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.