Unable to understand this lesson: Iterate Through All an Array's Items Using For Loops

Tell us what’s happening:

I can’t understand what is happening here.

function greaterThanTen(arr) { //this takes the array bellow 2, 12, 8, 14, 80, 0, 1 and stores it into the arr?
  let newArr = []; //this creates a let called newArr and assign a new variable to it?
  for (let i = 0; i < arr.length; i++) { //this set I to 0 as a starting point until the arr.lenght the length thus of the array bellow is reaches and increaments it with 1+ each time so  2 , 3 ,4 5??
    if (arr[i] > 10) { //this is  array and the coordinate of I is bigger then 10
      newArr.push(arr[i]); //this pushes the new array through the loop??
    }
  }
  return newArr;
}

greaterThanTen([2, 12, 8, 14, 80, 0, 1]);
// returns [12, 14, 80]

Then the instructions
what is elem?
element? is it an array? a string? a boolean? what is elem?
The more I try to read it the more confused.

Your code so far


function filteredArray(arr, elem) {
let newArr = [];
// Only change code below this line
for(let i = 0; i <arr.lenght; 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));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36.

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

Link to the challenge:

what are you referring to? there is nothing called elem in your code

what is it that you understand?

In the instructions:

elem represents an element that may or may not be present on one or more of the arrays nested within arr .

You don’t know in advance what type it is. It is a value that may or may not be in the array. That is all the information that is relevant to the challenge.

1 Like

Hi @KittyKora

  1. An element its the current data number(1,2,3) or string(‘john’), that is stored in an array, like this example, your creating a variable with an array
    var myArray = [element1,element2,element3];

var myArray = [1,2,3]; // this is your array with current elements 1,2,3
2. In the challenge you are passing all this elements as arguments of the array to the function called greaterThanTen([2, 12, 8, 14, 80, 0, 1]);
3. Then you are creating a new empty array called let newArr = []; and storing the elements that are greater than 10, by looping with a traditional for loop.
4. And your input will return not an empty new array but a new array with the current elements that are greater than 10.
let newArr[]; // [12,14,80] greater than 10.

Hope this helps.

1 Like

Yes it does for sure
But now I am stuck with the task: Such that any array nested within arr containing elem has been removed.
Simpley calling not equal to the nested array is not working.
And i’m not sure how to remove it.
I tried to find something that removes it
Best thing I could find was the delete keyword but, when i try to use it it fails
https://www.w3schools.com/js/js_object_properties.asp#:~:text=The%20delete%20keyword%20deletes%20both,effect%20on%20variables%20or%20functions.

function filteredArray(arr, elem) {
  let newArr = [];
  // Only change code below this line
for(let i = 0; i < arr.lenght; i++){ //base condition that goes through the loop
  if(arr[i] != elem){ 
    newArr.push(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));

  1. it’s length
  1. You are comparing an array with a number, instead you need to look inside the array if there is that number
1 Like