开发者

Javascript trouble parseFloat and if/ else loop

So working on some code and I'm not spotting the issue with my loop in the second function it keeps returning 40, I think the loop is written correctly. I'm guessing its the value passed within the if and else condition? if (par_hour < 40) which comes from parseFloat

 function addEmployee()
 {
 var employees = new Array();
 employees[0] = window.prompt("Enter employee name","Bob");

 var hours = new Array();
 hours[0] = window.prompt("Enter How many hours you have slaved away! ","4.5");

 var wages = new Array();
 wages[0] = window.prompt("Enter your hourly wages.","10.00");
 alert("test");
 calculateS开发者_JAVA技巧alary(hours,wages);
 alert("How about now");
 document.write(regHours);
 }

 function calculateSalary(hours,wages)
 {
 par_hour = parseFloat(hours[0]);
 par_wage = parseFloat(wages[0]);

 if (par_hour < 40)
   {
  regHours = par_hour;
   }
 else (par_hour >= 40)
   {
  regHours = 40;
 } 

  alert("why wont you work");
 }


 <html>
 <head>
 <title>Matt Beckley Week 2 Assignment</title>
 <script type ="text/javascript"  src="script1.js">
 </script>
 </head>
 <body>
 <h1>Weekly Gross Salary</h1>
 <table border="1">
 <tr>
 <th>Employees</th>
 <th>Total Hours</th>
 <th>Reg Hours</th>
 <th>Reg Pay</th>
 <th>OT Hours</th>
 <th>OT Pay</th>
 <th>Weekly Salary</th>  
 </tr>
 </table>
 <a href="salary.html" id="reload" onclick="addEmployee();">Add Employees</a>
 </body>
 </html>


Try changing the if statement like this:

  if (par_hour < 40)
  {
    regHours = par_hour;
  }
  else 
  {
    regHours = 40;
  } 


You are missing an if in the else if statement. Either remove the condition from the else part (as the condition is redundant: if it is not less than 40 it would be greater than or equal to 40), or make it else if (par_hour >= 40)

else
{
  regHours = 40;
}

//or
else if(par_hour >= 40)
{
  regHours = 40;
}

Edited off the part about missing return statements as it seems like you are using global variables. I'd suggest returning a value and using that instead of using global variables though.


else (par_hour >= 40)

should be

else if (par_hour >= 40)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜