Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get an error message that .replace is not a function? [duplicate]

I have this function:

function countLitreKgSums(cProductIds){
  var cLitreKgSums = new Array();
  var cWeek = 0;
  for(i=0;i<cProductIds.length;i++){
    var cLitreKgSum = 0;
    $("#plan_table td[class='week']").each(function(){
            cWeek = $(this).html();
            var cLitreKgValue = $("input[name*='plan_table_week" + cWeek + "_prod" + cProductIds[i] + "_']").val();
            if (cLitreKgValue == "") {
                cLitreKgValue = 0;
            }
            cLitreKgValue = cLitreKgValue.replace(/,/g, '.').replace(/[^\d\.]/g, '').replace(/\s/g, '');
            cLitreKgValue = parseFloat(cLitreKgValue);
            cLitreKgSum += cLitreKgValue;
       });
       cLitreKgSum = Math.round(cLitreKgSum * 100) / 100;
       cLitreKgSums[i] = cLitreKgSum;
  }
  return cLitreKgSums;
  //console.log(cLitreKgSums);
}

I get error message that .replace is not a function but in other functions it works as it should. What is the difference?

like image 907
anleon Avatar asked Jun 29 '10 13:06

anleon


1 Answers

Change this:

cLitreKgValue.replace(/,/g, '.')

to

("" + cLitreKgValue).replace(/,/g, '.')
like image 138
Jonathon Faust Avatar answered Oct 14 '22 21:10

Jonathon Faust