Basic Data Structures:for loop(problem)

function filteredArray(arr, elem) {
  let newArr = [];
  let len = arr.length;
  //console.log(len);
  // change code below this line
  for(let i = 0;i<len;i++){
    //console.log(arr[i]);
    let doulen = arr[i].length;
    //console.log(doulen);
    for(let j = 0;j<arr[i];j++){
      console.log(arr[i][j]);
      if(arr[i][j] !== elem){
        newArr.push(arr[i][j]);
      }
    }
  }
  console.log(newArr);
  // change code above this line
  return newArr;
}


// change code here to test different cases:
console.log(filteredArray([[1,1,1],[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));

Looks clean but not working

Are you sure this condition is correct for your inner loop ?

arr[j] takes the looped values of inner arrays of arr.

@topcoder

Hint: I notice you created doulen but you do not use it anywhere.

There was a mistake with the length of the inner array of arr

console.clear();
function filteredArray(arr, elem) {
  "use strict";
  let newArr = [];
  let len = arr.length;
  //console.log(len);
  // change code below this line
  for(let i = 0;i<len;i++){
    //console.log(arr[i]);
    let doulen = arr[i].length;
    //console.log(doulen);
    for(let j = 0;j<arr[i].length;j++){
      console.log(arr[i][j]);
      if(arr[i][j] !== elem){
        newArr.push(arr[i][j]);
      }
      
      
    }
  }
  console.log(newArr);
  // change code above this line
  return newArr;
}


// change code here to test different cases:
console.log(filteredArray([[1,1,1],[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));

This is revised code ,its returning the values which are equal to the elem;

You should not be pushing the elements of the sub arrays to newArr. Instead you should be pushing the entire sub array to newArr. ONLY if the sub array does not contain contain elem.

You need to rethink your algorithm here.

Below I have taken your current code and removed the comments and console.log statements and anything that is incorrect in your current code’s logic. In its place, I have created comments which are part of the logic you could use for code which would pass the tests.

function filteredArray(arr, elem) {
  "use strict";
  let newArr = [];
  let len = arr.length;
  for(let i = 0;i <len; i++){
    // Assume that all of the elements of the subarray do not match elem (initialize a Boolean flag variable to true)
    for(let j = 0;j<arr[i].length;j++){
      // Check if the current element in sub array is equal to elem.
      // If it is equal, then set the value of your flag variable to be false AND stop break out of loop
      } 
    }
    // If the flag variable is still true then push the sub array into newArr
  }
  return newArr;
}

This is the code






function filteredArray(arr, elem) {
  "use strict";
  let newArr = [];
  let len = arr.length;
  for(let i = 0;i <len; i++){
    /* Assume that all of the elements of the subarray do not match elem (initialize a Boolean flag variable to true)*/
    if(arr[i].indexOf(elem) === -1){
      return true;
    }
    for(let j = 0;j<arr[i].length;j++){
      // Check if the current element in sub array is equal to elem.
      if(arr[i][j] === 3){
        return false;
        break;
      }
      /* If it is equal, then set the value of your flag variable to be false AND stop break out of loop*/
      } 
    }
    // If the flag variable is still true then push the sub array into newArr
 newArr.push(arr[i]);
 console.log(newArr);
  return newArr;
}
console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));

You should not be returning a value here, because that exits the function immediately. I said to create a Boolean value here.

Similar problem here with the return statement. Also, you do not always want to compare the subarray element to the number 3 do you?

It worked






function filteredArray(arr, elem) {
  "use strict";
  let newArr = [];
  let len = arr.length;
  let value ;
  for(let i = 0;i <len; i++){
    /* Assume that all of the elements of the subarray do not match elem (initialize a Boolean flag variable to true)*/
    if(arr[i].indexOf(elem) === -1){
     value = true;
    console.log(value);
    }
    for(let j = 0;j<arr[i].length;j++){
      // Check if the current element in sub array is equal to elem.
      if(arr[i][j] === elem){
         value = false;
         console.log(value);
         break;
      }
      /* If it is equal, then set the value of your flag variable to be false AND stop break out of loop*/
      } 
     if(value === true){
       newArr.push(arr[i]);
     } 
    }
    // If the flag variable is still true then push the sub array into newArr
  return newArr;
}
console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));

But is it the right approach.

Well if you are going to use the indexOf, then you only need a single for loop.

  for(let i = 0;i <len; i++){
    if(arr[i].indexOf(elem) === -1){
      newArr.push(arr[i]);
    }
  }