Tell us what’s happening:
Hello,
Often times I find myself overthinking practice problems and writing out unnecessarily long programs when a simple function will do exactly what I want. The below is a good example of this.
Generally I am able to find a solution through the method that I am trying to use. But when I check my answer against the FCC provided answer, many times I find I’ve done it in a different and usually less optimal way.
Is there any way to train myself start thinking more efficiently when I work through problems, or is it just something that comes with time?
Usually when I find a different answer than the one I was expecting, I will close the solution and work my way through it using the method they’ve shown. I am just worried I continually seem to be doing things the hard way.
**Your code so far**
function filteredArray(arr, elem) {
let newArr = [];
// Only change code below this line
for (let i = 0; i < (arr.length); i++) {
let count = 0;
for (let j = 0; j < arr[i].length; j++) {
if (arr[i][j] == elem) {
count += 1;
} else if (count == 0 && j == arr[i].length - 1) {
newArr.push(arr[i]);
}
}
}
return newArr;
// Only change code above this line
}
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/101.0.4951.67 Safari/537.36
Challenge: Iterate Through All an Array’s Items Using For Loops
Link to the challenge: