HELP_Where do I belong

HELP_Where do I belong
0

#1

Tell us what’s happening:
I’m getting the error message
TypeError: newArr.sort is not a function

Your code so far


Insert the value (second argument) into the array.
Sort the array with the second argument in it.
Find the index of the second argument.

*/



function getIndexToIns(arr, num) {
  var newArr = [];
  newArr = arr.push(num);
  newArr.sort(function(a, b){
    return a - b;
  });
  var index;
  index = newArr.findIndex(num);
  return index;
}



getIndexToIns([40, 60], 50);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36.

Link to the challenge:


#2

The value return by the push() method is the length of the new array (MDN reference). As such, the following in in your code is actually not creating a new array:

newArr = arr.push(num);

What it’s actually doing is this:

// ...
console.log(arr, num); // [40, 60], 50
newArr = arr.push(num);
console.log(arr); // [40, 60, 50]
console.log(newArr); // 3
// ...

And that is the reason why you get TypeError: newArr.sort is not a functionnewArr in your code is a number and not an array.

I hope that helps. :slight_smile:


#3

/*Pseudocode:

Insert the value (second argument) into the array.
Sort the array with the second argument in it.
Find the index of the second argument.

*/

function getIndexToIns(arr, num) {
  arr.push(num);
  arr.sort(function(a, b){
    return a - b;
  });
  function findNumber(num) { /*findNumber because the documentation specified a testing function to be specified. Could I use findIndex(num) directly? */
}
  var index;
  index = arr.findIndex(findNumber);
  return index;
}

getIndexToIns([40, 60], 50);

#4

The below does not satisfy any criteria. Returns -1. Why?

function getIndexToIns(arr, num) {
  arr.push(num);
  arr.sort(function(a, b){
    return a - b;
  });
  function findNumber(num) { 
}
  var index;
  index = arr.findIndex(findNumber);
  return index;
}

#5

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#6

You have specified a function called findNumber for the callback function parameter of the findIndex function. The findIndex function executes the callback function you specify once for each array index until it finds an index where the callback returns a truthy value. If the callback never returns a truthy value, then -1 is returned.

The callback function of findIndex has 3 possible arguments, with the 1st being the current element being processed in the array, the 2nd being the index of the current element being processed in the array and the 3rd being the array in which findIndex was called upon.

Your function findNumber has one parameter (num) which will become the element being processed in the array, because it is the 1st argument of the callback function. The problem is your function does not explicitly return a value (you only have a commented line). When a function does not have a return value, JavaScript returns undefined automatically. The value undefined is a falsy value, so every single element causes findNumber to return false. By the definition I explained at the top about what happens when the callback function never returns a truthy value, the findIndex function ends up returning -1 which is assigned to index.

You are on the right track, but you need to make use of the num argument in the getIndexToIns function in conjunction with the argument in findNumber. Because they have the same name and need to be used in the same function (findNumber), I suggest changing the one in findNumber to a different name.