Please clarify this

I understand that -1 is the response from a indexOf when elem does not exist, so we’re checking this to log the ones that don’t exist, but how is the result of he equation below the following:

[ [ 10, 8, 3 ], [ 14, 6, 23 ] ] — doesn’t [ [ 10, 8, 3 ] have index of 0 and [ 14, 6, 23 ] ] index of 1?

If it was filteredArray([ ["trumpets", 2], ["flutes", 4], ["saxophones", 2] ], 2) should return [ ["flutes", 4] ] I thought it was logging [ ["flutes", 4] ] because it elem was 4 and not 2, but it appears not.

Your code so far


function filteredArray(arr, elem) {
let newArr = [];
// Only change code below this line
for (let i = 0; i < arr.length; i += 1) {
  if (arr[i].indexOf(elem) === -1) {
    newArr.push(arr[i]);
  }
}
// Only change code above this line
return newArr;
}

console.log(filteredArray([[10, 8, 3], [14, 6, 23], [3, 18, 6]], 18));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36.

Challenge: Iterate Through All an Array’s Items Using For Loops

Link to the challenge:

you built a new array from the subArrays which do not contain elem. I didn’t quite understand your question here?

Nono, you understand it correctly I think (with the flutes one, yes, it’s because that array doesn’t contain 2). So for the other example: the loop goes through each element in the main array. There are three elements. Each of those elements happens to be an array. If that subarray contains the given value, then ignore it. So

  1. arr[0] is [10,8,3], that doesn’t contain 18 (indexOf(18) is -1), push it.
  2. arr[1] is [14,6,23], that doesn’t contain 18 (indexOf(18) is -1), push it.
  3. arr[2] is [3,18,6], that does contain 18 (indexOf(18) is 1), don’t push it

End result: [[10,8,3], [14,6,23]] (those two elements that don’t contain 18 get pushed to a new array)

So same thing:

  1. arr[0] is ["trumpets", 2], that does contain 2 (indexOf(2) is 1), don’t push it.
  2. arr[1] is ["flutes", 4], that doesn’t contain 2 (indexOf(2) is -1), push it.
  3. arr[2] is ["saxophones", 2], that does contain 2 (indexOf(2) is 1), don’t push it.

End result: [["flutes", 4]] (that one element that doesn’t contain 4 gets pushed to a new array).

3 Likes

Impeccable explanation. Thank you for taking time to respond to me.

And if it was [18, 3, 6] it wouldn’t contain 18 because elem is located at the 1 or higher index due to (arr, elem), corect?

2 Likes

Yep (I’m assuming you meant would contain, not wouldn’t contain!)

Ok so the placement of 18 doesn’t matter?

I had thought since we said elem === -1 and since function filteredArray(arr, elem) places “elem” after “arr”, the 18 would have to come at 1 or higher position (not the first)

[[10, 8, 3], [14, 6, 23], [3, 18, 6]] being arr
18 being elem
filteredArray([[10, 8, 3], [14, 6, 23], [3, 18, 6]], 18))

If you check for -1 for indexOf, the position of 18 doesn’t matter.
[3, 18, 6] will return 1
[18, 3, 6] will return 0

The second question, I didn’t understand.
I mean the order of parameter doesn’t matter.
You can write function filteredArray(elem, arr) too, but you must give it the correct value:
filteredArray(18, [[10, 8, 3], [14, 6, 23], [3, 18, 6]] )

1 Like

If the element is not in the array, indexOf will return -1. If it is, it will return a number, and what that number is is irrelevant beyond it not being -1.

The condition is just a way of saying “is this element not in this array”.

Same thing, possibly clearer:

function filteredArray(arr, elem) {
  let newArr = [];
  // Only change code below this line
  for (let i = 0; i < arr.length; i += 1) {
    if (arr[i].includes(elem) === false) {
      newArr.push(arr[i]);
    }
  }
  // Only change code above this line
  return newArr;
}

So that condition is false if the array includes whatever elem is, and if that is the case the code in the block doesn’t run, and nothing gets pushed.

What position the element is at isn’t relevant to the challenge: the condition just checks whether it is present or not.

I think you’re misunderstanding something more fundamental than this though, as you’re talking about placement, and that doesn’t really have anything to do with this challenge

1 Like

@DanCouper yeah makes sense. I was trying to understand the problem. I’m not good with the terms yet so I’m having a tough time remembering things and just going through the course.

It seems impossible for me to just do one or two problems with each concept and really know what I’m doing unless I practice each concept 5+ times in varies ways through a longer period of time. I’m continuing to go through this stuff to understand, but memorizing is not something I’ve been able to do. So even now when you’re saying placement, parameter, etc I’m not sure so I said position.

For loops in general is something I need much much more practice with

1 Like