Alternate to JS built-in methods

Tell us what’s happening:
Describe your issue in detail here.
Can this be solved without using any built-in Javascript methods? like two for loops?

  **Your code so far**

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.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));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15

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

Link to the challenge:

I added spoiler tags around your working solution for those who have not solved this challenge yet.

Are you asking if you can do this without using indexOf or any other array methods explicitly?

If that is your question, then yes it can be done, and you’re exactly right - a nested loop. Here might be a pseudocode view of how it could work:

Within the *outer* loop:
 - create a variable that will be either true or false, 
  indicating whether the elemwas found in this
  sub-array. Initially, set it to false as elem has
   not been found.
- loop over this sub-array and
  - check if the current member of this
    sub-array matches elem.
  - if it does, change our Boolean variable to true.
- after looping over the entire sub-array, if our 
  Boolean variable is still false, elem was not found. 
- Handle either found (true) or not found (false).

Continue until outer loop completes.
1 Like

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