Javascript麻烦parseFloat和if / else循环

时间:2010-01-25 09:51:33

标签: javascript

所以处理一些代码并且我没有在第二个函数中发现我的循环问题它一直返回40,我认为循环是正确编写的。我猜它在if和else条件下传递的值是多少? if(par_hour< 40)来自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");
 calculateSalary(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>

3 个答案:

答案 0 :(得分:3)

尝试更改if语句:

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

答案 1 :(得分:0)

else (par_hour >= 40)

应该是

else if (par_hour >= 40)

答案 2 :(得分:0)

您在if声明中遗漏了else if。从else部分中删除条件(因为条件是多余的:如果它不小于40则大于或等于40),或者使其成为else if (par_hour >= 40)

else
{
  regHours = 40;
}

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

关于缺少返回语句的部分编辑,因为您似乎正在使用全局变量。我建议返回一个值并使用它而不是使用全局变量。