为什么我会得到".push not a function"(.push not a function)的信息? [英] Why do I get ".push not a function"?

查看:71
本文介绍了为什么我会得到".push not a function"(.push not a function)的信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码怎么了?

function longestConsec(strarr, k) {
  var currentLongest = "";
  var counter = 0;
  var outPut = [];

  if(strarr.length === 0 || k > strarr.length || k <= 0){
    return "";
  }
  for(var i = 0; i < strarr.length; i++){
    if(strarr[i] > currentLongest){
      currentLongest = strarr[i];
    }
  }
  while(currentLongest !== strarr[counter]){
    counter = counter + 1
  }
  for (var j = 0; j < k; j ++){
    outPut = outPut.push(strarr[counter + j]);
  }

  outPut = outPut.join("");

   return outPut;
}

我不断收到"outPut.push不是函数".

I keep on getting "outPut.push is not a function".

推荐答案

数组推入函数在推入后返回数组的长度.

Array push functions returns the length of the array after pushing.

所以,在您的代码中

outPut = outPut.push(strarr[counter + j]);

outPut现在是一个数字,而不是数组,因此第二次遍历循环时,outPut不再具有push方法.

outPut is now a number, not an array, so the second time through the loop, outPut no longer has a push method.

一个简单的解决方案是将该行更改为

A simple solution is to change that line to

outPut.push(strarr[counter + j]);

这篇关于为什么我会得到".push not a function"(.push not a function)的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆