freecodecamp.cn icon indicating copy to clipboard operation
freecodecamp.cn copied to clipboard

TypeError: arr.push(...).sort is not a function,why?

Open joezhoux opened this issue 9 years ago • 2 comments

Challenge Where do I belong has an issue. User Agent is: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.22 Safari/537.36 SE 2.X MetaSr 1.0. Please describe how to reproduce this issue, and include links to screenshots if possible.

My code:

function where(arr, num) {
  // Find my place in this sorted array.
   arr.push(num).sort(function(a,b){
     return a-b;
   });
   
   
  return arr.indexOf(num);
}

where([40, 60], 50);

joezhoux avatar Feb 08 '17 00:02 joezhoux

function where(arr, num) { arr.push(num); arr.sort(function(a,b){return a-b;}); return arr.indexOf(num);}

inetsurfer avatar Feb 26 '17 14:02 inetsurfer

因为数组push方法返回的是更新后的·数组length,并不是数组对象,所以没有sort方法。

jiangtao-zzz avatar Feb 07 '18 08:02 jiangtao-zzz