Everytime i change the filteredArray input to test the cases that are tested on the bottom left, i get the correct output. But i fail the test. Here is a picture showing in the console that i am getting the correct output. Am i missing something?
Your console is not showing the correct result. Look at the bottom right of that image: what it shows is an array in an array in an array: 3 levels deep.
newArr.push([arr[i]]): with this line, you are pushing an array containing an array into your main array.
Also, out of curiousity, why labels? Have you got some extremely old teaching material that you’ve been referring to?
I googled break points when i was still having trouble getting it to work and an example used labels. I just took the labels off and it broke out of the first for loop like it was supposed too.
Thanks for catching my error. It had been racking my head. I also see how I wasn’t comprehending the console output now.
I decided to just now look at the hint section. How do i improve and not make my code overly complicated like i just did? The hint shows smaller, faster, and less bloated code than mine.
Note: Labeled loops or blocks are very uncommon. Usually function calls can be used instead of loop jumps.
So one clean way to complete this challenge is to do just that - write a function that checks if a value is in an array (which is what your inner loop does), and use that function:
function arrayIncludes(arr, elem) {
for (let i = 0; i < arr.length; i++){
if(arr[i] === elem){
return true;
}
}
return false;
}
function filteredArray(arr, elem) {
let newArr = [];
for (let i = 0; i < arr.length; i++){
if (!arrayIncludes(arr[i], elem)) {
newArr.push(arr[i]);
}
}
return newArr;
}
Noe that the function is checking for prescence, not omission - arrayIncludes rather than arrayDoesNotInclude (then saying if NOT arrayIncludes in the condition) - because:
Checking an array contains a value is quite a common operation though, and JS provides a function for that:
function filteredArray(arr, elem) {
let newArr = [];
for (let i = 0; i < arr.length; i++){
if (!array[i].includes(elem)) {
newArr.push(arr[i]);
}
}
return newArr;
}
There are many other ways to do this. Using labels to control loops is one way, it’s just that labels are incredibly uncommon in JS, for good reason, as there is [afaik] never a need to use them. Programming using GOTOs/jump statements is not something that is recommended outside of quite specific things - the advice against that style of programming goes back at least 50 years.