最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why do I get ".push not a function"? - Stack Overflow

programmeradmin2浏览0评论

What's wrong with my code?

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;
}

What's wrong with my code?

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;
}

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

Share Improve this question edited Jan 23, 2018 at 9:10 Peter Mortensen 31.6k22 gold badges109 silver badges133 bronze badges asked Jan 23, 2018 at 4:22 Braden FoltzBraden Foltz 1271 gold badge1 silver badge7 bronze badges 7
  • 10 array.push returns the length of the array after pushing ... after the first push, outPut is now a NUMBER not an ARRAY - simply change outPut = outPut.push(strarr[counter + j]); to outPut.push(strarr[counter + j]); – Jaromanda X Commented Jan 23, 2018 at 4:23
  • Also, k <= 0 is possibly k =< 0 – suchislife Commented Jan 23, 2018 at 4:28
  • because someone did post a "code only" answer, they deleted it rather than explain it, thought they may want to explain it, so why post an answer as well :p – Jaromanda X Commented Jan 23, 2018 at 4:29
  • 1 @Vini - no, not at all – Jaromanda X Commented Jan 23, 2018 at 4:29
  • 3 Why the name "outPut"? Is "output" reserved? – Peter Mortensen Commented Jan 23, 2018 at 9:11
 |  Show 2 more comments

3 Answers 3

Reset to default 15

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

So, in your code

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

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]);

Array.push

adds one or more elements to the end of an array and returns the new length of the array.

And you have this line:

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

You're adding an element to outPut.push(strarr[counter + j]); and then reassigning outPush to the length of the array.

You should only call the push method on the array and a new element will be added:

for (var j = 0; j < k; j ++){
    outPut.push(strarr[counter + j]);
}

Array.push() returns the length of that particular array. In your code, you assign a number to outPut. Now when the loop runs for the second time, outPut is no more an array but a number, hence you get the error.

You check it by logging outPut to the console. You will see that

for (var j = 0; j < k; j ++){
    outPut = outPut.push(strarr[counter + j]);
    console.log(outPut);
}

It will show:

 1
  VM696:18 Uncaught TypeError: outPut.push is not a function
    at longestConsec (<anonymous>:18:21)
    at <anonymous>:1:1

All you need to do is change it to:

for (var j = 0; j < k; j ++){
    outPut.push(strarr[counter + j]);
}
发布评论

评论列表(0)

  1. 暂无评论