Basic Data Structures - Iterate Through All an Array's Items Using For Loops

Hi there , just wondering why this code doesn’t work

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

console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));


i got it to work by using push() function however i don’t see why the assignment of
newArr[i]= arr[i] doesn’t achieve the same result
is this due to the newArr array being empty, and thus not having an i element to change ? can an empty array not be assigned?

If you toss some console logs around you might be able to see what is going on. For example if i had an empty let array = [], then lets say i put another array inside it at index 5 ['something'], what is array going to look like? Does that help you understand what is going on with newArr[i]=arr[i] vs. just pushing a value into an empty array?

if im honest i still don’t understand the difference if i push an array inside it or if i assign an array to an index value , personally i would think it’s the same result i.e a nested array …

however i understand that if i assign something at value [5] in an array then positions [0] through [4 ] must also be filled meaning allowing this sort of assignment could be troublesome
i’d like to rephrase my question

can i assign values of an empty array using the assignment operator =? or is it not allowed and i must use push/ unshift?

it is allowed but not good practice
The reason is that push will automatically add things to the end of the array but trying to keep track of the last index yourself and use it in bracket notation is bound to cause problems for you in the future if you get used to doing things like that.

console.log(filteredArray([[3, 2, 3], [1, 6, 8], [3, 13, 26], [19, 3, 9]], 3));

Try this. What do you see if you use the version of your code with =

thats the reason i posed this question my console returns empty
which is what i didnt understand and therefore assumed my assignment operator wasnt working

As mentioned above using = is working, there is no reason you cant do that, but you really should not do that. Your code is doing what the test asks, for the most part. If elem is in the sub array ignore it, if elem is missing add it to the return array. In that function call all sub arrays have a 3 in them, and elem is 3, so none of them should be added to the returned array. It should be empty.

The reason your code is failing just the one test with = is because of how you are adding the sub arrays without elem. A push will always add to the end of an array. But if you notice in newArr[i]=arr[i], i might not be the index for the end of the array. You got lucky with the other three test arrays because the index of i just happened to be at the end of the empty array.

let emptyArrayIndex = [];
emptyArrayIndex[5] = ['sub array 1'];
emptyArrayIndex[2] = ['sub array 2'];
console.log(emptyArrayIndex); //[ , , [ 'sub array 2' ], , , [ 'sub array 1' ] ]
let emptyArrayPush = [];
emptyArrayPush.push(['sub array 1']);
emptyArrayPush.push(['sub array 2']);
console.log(emptyArrayPush); //[ [ 'sub array 1' ], [ 'sub array 2' ] ]

So when its testing filteredArray([["trumpets", 2], ["flutes", 4], ["saxophones", 2]], 2) the index you are putting flutes is 1, so your return array is [ , [ 'flutes', 4 ] ], not [["flutes", 4]] like the test expects.

ahha i seee its a matter of order in the array where push gets it right on every instance or rather i am adding unnecessary empty indexes in my array
thank you so much !!

That’s why I was asking you to try this test case, you should have seen a gap in the middle of the array.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.