2014-11-17 3 views
0

Я получаю сообщение об ошибке следовать в моей консоли: «Uncaught TypeError: не определено не является функцией»Невозможно вызвать toFixed по ряду переменной

<script> 
    //name : calculateResult()--> 

    function calculateResult() { 
     console.log("calculateResult() function called!"); 

     //1. Declare Variables--> 
     var hoursWorked, 
      jobCategory, 
      jobCategorySelectedIndex, 
      hoursEligibleForBasePay, 
      hoursEligibleForOvertime, 
      basePayAmount, 
      overtimePayAmount, 
      totalPayAmount, 
      overtimePayRate; 

     //2. Values for Local Variables--> 
     hoursWorked = document.getElementById("txthoursWorked").value; 
     console.log("hoursWorked = " + hoursWorked); 

     //Get Select element choice: Job Category--> 

     jobCategorySelectedIndex = document.getElementById("seljobCategory").selectedIndex; 
     console.log("jobCategorySelectedIndex = " + jobCategorySelectedIndex); 

     jobCategory = document.getElementById("seljobCategory").options[jobCategorySelectedIndex].value; 
     console.log("jobCategory = " + jobCategory); 

     //3. Do Calculations--> 
     hoursWorked = parseFloat(hoursWorked); 

     if (jobCategory == "M") { 
      basePayRate = "25"; 
     } else if (jobCategory == "R") { 
      basePayRate = "20"; 
     } else if (jobCategory == "S") { 
      basePayRate = "15"; 
     } 

     hoursEligibleForBasePay = 40; 
     basePayAmount = basePayRate * hoursEligibleForBasePay; 
     console.log("basePayAmount = " + basePayAmount); 
     console.log("hoursEligibleForOvertime =" + hoursEligibleForBasePay); 

     if (hoursWorked > 40) { 
      hoursEligibleForOvertime = hoursWorked - 40; 
     } else { 
      hoursEligibleForOvertime = 0; 
     } 
     console.log("hoursEligibleForOvertime = " + hoursEligibleForOvertime); 


     overtimePayRate = 1.5 * basePayRate; 
     overtimePayAmount = overtimePayRate * hoursEligibleForOvertime; 
     totalPayAmount = basePayRate + overtimePayAmount; 

     console.log("overtimePayRate = " + overtimePayRate); 
     console.log("overtimePayAmount = " + overtimePayAmount); 
     console.log("totalPayAmount = " + totalPayAmount); 

     //4. Display Results--> 
     displayString = "Base Pay " + "$" + basePayAmount.toFixed(2) + "<br />" + 
      "Overtime Pay &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + "$" + overtimePayAmount.toFixed(2) + "<br />" 
     "Total Pay &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + "$" + totalPayAmount.toFixed(2); 

     document.getElementById("divDisplay").innerHTML = displayString; 



    } 
</script> 

ошибка на дисплее строки в строке Total PayAmount

любые идеи?

+0

Я думаю, что идентификатор отсутствует в вашем DOM. –

ответ

0

Фактическая ошибка на самом деле отсутствует.

totalPayAmount определяется здесь:

totalPayAmount = basePayRate + overtimePayAmount; 

basePayRate определяется здесь:

if (jobCategory == "M") { 
    basePayRate = "25"; 
} else if (jobCategory == "R") { 
    basePayRate = "20"; 
} else if (jobCategory == "S") { 
    basePayRate = "15"; 
} 

Так basePayRate строка. Тогда totalPayAmount также является строкой, которая не имела бы метода toFixed.