Challenge 14 Megathread

Welcome to challenge #14!

Use this thread for any and all questions relating to challenge 14

1 Like

Should probably add to the challenge that the function requires you to return the toggleList.

3 Likes

@patrickgg98 Perhaps at the end, we have to put all functions to run together.

1 Like

Yeah could be possible, but like for the purposes of this challenge alone it should be mentioned if people aren’t able to see immediately why their tests are failing!

2 Likes

I’m very happy, I actually got this within 15 minutes. I have 4 lines and as such I could probably refactor it to be more efficient but I got it and that’s that.

5 Likes

The more challenge I work on, the more confident I feel about solving challenges and I cannot believe it is day 14 already !!!

7 Likes

Even though the example’s output shows a list of objects, it’d be better if it was explicitly stated.

1 Like

Gaining confidence!
forEach loop
if statement to check the boolean and update the boolean within the loop
return the array

If you are doing another loop, for…of loop works as well

I don’t think you even need to check the boolean. Just make every toggle’s .isOn property be true.

4 Likes

Makes sense, regardless of the switch’s initial state, making all of them true will solve the problem.
I did not think of this and took the approach of only changing the ones that were false.

This challenge reminded me of the song Lisztomania by Phoenix lol I am not sure if it was intentional or I am just a fan :sweat_smile:

2 Likes

change the value of the isOn property to true for every switch in the list, and then return the updated array.

I think you missed it here…usually @LHL-Team is kind enough to give us the hint within the question.

1 Like

I was able to make the code work with a “for” loop:

for (let i = 0; i < toggleList.length; i++)

But I want to know how to make it work with forEach as well. I had tried this and couldn’t get it to pass the last test:

toggleList.forEach(function (isOn) {
  toggleList.isOn = true;
})

I’m still proud of myself for starting to understand how to use the for loop though!

5 Likes

Not sure where I’m going wrong here. A point in the right direction would help, thanks.

const switchAllTogglesOn = (toggleList) => {
for (let i = 0; i < toggleList.length; i++){
toggleList.isON = true;
}
return toggleList
}

I think you’ve misspelled isOn (isON)

Good catch, just fixed it, and it’s still not working.

Hm I’m stuck in the same spot as you. Also tried a forEach but that isn’t working either. Let me know if you figure it out!

Hey everyone,

I am a little stuck.

I tried a forEach and added the if statement

if (toggleList.isOn === false) {
toggle.isOn = !toggle.isOn;
}
});
and returned the toggleList

It passes the first two tests but fails the third… i think it might be changing all of the isOn instead of the one that has a false value… I’m new to JS and I am unsure where to look to help correct my code.

it should be toggleList[i].isOn = true inside the for loop…should work after this

4 Likes

Thanks! That helped!

1 Like