修复未捕获的TypeError:“您的函数”不是函数

时间:2019-06-27 14:40:29

标签: javascript

我有一个简单的代码来填充某些条件的数组,但是每次遇到此错误时:

  

未捕获的TypeError:int_part不是函数

这是什么问题?我想念什么?

代码如下:

var totalSlides = 4; // total number of slides we have
var lastSlideInt = 20; // numbers of review interactions on the last slide

var n = totalSlides - 1;
var x = lastSlideInt / (n * (n + 1) / 2);

var cal = [];
for (var i = 1; i < lastSlideInt; i++) {
  cal.push(i * x);
}

var indexs = [];
var minus = [];
//
var sum = 0;
var floatSum = 0;
for (var i = 0; i < cal.length; i++) {
  sum += cal[i];
  if (sum >= 1) {
    var pusher = int_part(sum) + int_part(floatSum);
    floatSum += float_part(sum);
    minus.push(pusher);
    indexs.push(i);
  }

}
//


function int_part(y) {
  return int_part = Math.trunc(y);
  float_part = Number((y - int_part).toFixed(2));
}

function float_part(d) {
  int_part = Math.trunc(d);
  return float_part = Number((d - int_part).toFixed(2));
}

console.log(int_part(0.2));

我认为错误应该在if语句的某个位置:

 if (sum >= 1){
      var pusher = int_part(sum) + int_part(floatSum);
      floatSum += float_part(sum);
      minus.push(pusher);
      indexs.push(i);
    }

1 个答案:

答案 0 :(得分:1)

您正在将功能int_partfloat_part重新分配给功能体内的数字。您应该只返回值。

var totalSlides = 4;    // total number of slides we have
var lastSlideInt = 20;  // numbers of review interactions on the last slide

var n = totalSlides - 1;
var x = lastSlideInt / (n*(n+1)/2);

var cal = [];
for (var i = 1; i < lastSlideInt; i++ ){
     cal.push(i * x); 
}

var indexs = [];
var minus = [];
//
var sum = 0;
var floatSum = 0;
for (var i = 0; i < cal.length; i++) {
    sum += cal[i];
    if (sum >= 1){
      var pusher = int_part(sum) + int_part(floatSum);
      floatSum += float_part(sum);
      minus.push(pusher);
      indexs.push(i);
    }

}
//


function int_part(y){
  return Math.trunc(y);

}

function float_part(d){
  let int_part = Math.trunc(d);
  
  return Number((d-int_part).toFixed(2)); 
}

console.log(int_part(0.2));