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

Tell us what’s happening:
Describe your issue in detail here.
hey, someone can help me to understand more about this challenge?
Your code so far

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

console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));
function filteredArray(arr, elem) {
  let newArr = [];
  // Only change code below this line
  for(let i=0; i<arr.length; i++){
    for(let k=0; k<arr[i].length;k++){
      if(arr[i][k]!==elem){
        newArr.push(arr[i][k]);
      }
    }
  }
  // Only change code above this line
  return newArr;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:105.0) Gecko/20100101 Firefox/105.0

Challenge: Basic Data Structures - Iterate Through All an Array’s Items Using For Loops

Link to the challenge:

Can you be more specific about what you are not understanding?

Modify the function, using a for loop, to return a filtered version of the passed array such that any array nested within arr containing elem has been removed.
can you help me to understand how to solve it using two loop ? i try it but it doesn’t pass

i did not understand the way other people have solve the challenge that why i want to solve it the way i can understand

Try to think about how you are solving this. Write it out in pseudo code. What you have so far

create newArr, empty array
loop over arr (elements are subArr)
  loop over subArr (elements are num)
  if num does not equals elem
    add num to newArr

return newArr

This issue is … what should you be adding num to? Should it be newArr? That would make newArr an array of numbers. But if you look at the expected answers, they are expecting an array of arrays of numbers. In other words, you should end up with the same structure, just with some elements removed. So, you shouldn’t be pushing numbers onto newArr. You should be pushing something else. Where you have the push, that can work, but not onto newArr. You will have to push something else onto newArr in a slightly different place. Remember, you are cleaning up the sub arrays.

Work with those hints and see if it gets you closer. Maybe (and I highly recommend it), try writing out some pseudo code. If you get stuck, check back.

That’s where Am stuck, how to store those numbers into an array of arrays.

for(let i=0; i<arr.length; i++){
for(let j=0; j<arr[i].length; j++){
if(arr[i][j]!==elem){
newArr.push(arr[i][j]);
}
}
}
i don’t why when it comes to challenges am stucked every day.

OK, a further hint…

Instead of thinking about storing the numbers in an array of array, create each sub array and then when ready, add those to the super array.

You have to check all of the elements in the nested array before you can push anything. Otherwise, you will just push the numbers that are not elem when you should be skipping the array completely.

One option might be using a boolean flag that you set to true/false and only push the nested array if the flag has the correct value.


Did you look at the docs for indexOf or includes? The methods shouldn’t be that hard to understand and they save you from having to implement that logic yourself. Not that there is anything wrong with also coding a version without them which I would suggest trying to do as well.

try to show me how to solve the challenge. The problem is when I want to output the number which are not = to elem. that where am stuck
.

You have a big boat with crates of apples. You want to create a new boat with crates of apples but only apples that aren’t green. So, you’ve created your new big boat. The problem is that you are putting the non-green apples directly into the new big boat - you should be creating new crates (one for each crate on the old boat) and putting the non-green apples in those, and then placing each new crate onto the new boat.

Write out what you are trying to do in pseudocode.

the issue is that, I don’t know how to put those non-green apples into the new crates of the new boat.


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

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

please try to explain me how to do it clearly

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

You need to undo the changes you made below the line.

You’re only going to create one boat. You’re going to need to create a new crate for each old create, so which loop does that go in? When do you add the new crate to the new boat?

I pseudo code:

create newBoat

loop over oldCrate(s) in oldBoat
  make newCrate

  loop over apple(s) in oldCrate
    if apple is not green
      place apple in newCrate

  place newCrate in newBoat

return newBoat
type or paste code here
function filteredArray(arr, elem) {
  let newArr = [];
  // Only change code below this line
 
  for(let i=0; i<arr.length; i++){
 let newArr1=[];
    for(let j=0; j<arr.length; j++){
      
      if(arr[i][j]!==elem){
        newArr1.push(arr[i][j]);
      }
    }
     newArr.push(newArr1);
  }
 
  // Only change code above this line
  return newArr;
}

console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));*[quote="kevinSmith, post:16, topic:560132, full:true"]
I pseudo code:

create newBoat

loop over oldCrate(s) in oldBoat
make newCrate

loop over apple(s) in oldCrate
if apple is not green
place apple in newCrate

place newCrate in newBoat

return newBoat
[/quote]

*this is what i have done it does not pass but it remove all the non-green apples but return them as undifined in the arrays. why?

this is what i have done it does not pass but it remove all the non-green apples but return them as undifined in the arrays. why?

Actually, I’ve been giving you bad advice. I was getting my challenges mixed up. The logic should be:

create newBoat

loop over crate(s) in oldBoat
  if crate does not contain green apple
    place crate in newBoat

return newBoat

The question is how do you figure out if the crate contains a green apple. Lasjorg mentioned the includes method - that would be an easy way. If you do that, you’ll only have one loop (although, technically the includes has a loop hidden inside). You could also do it without includes, using a for loop.

I need to learn more about methods js.
explain me in deep about this the condition

type or paste code here
if(arr[i]!==elem){
newArr.push(arr[i]);
}