Challenge 14 Megathread

Thank you, that helped me too!

It says return the updated array.

The way I did this was to seperate the function from .forEach but I think the way you want to do it inline where it says toggleList.isOn should be isOn.isOn
What ever you put inside the () after function is data that the function uses. So if you want to use
toggleList.isOn = true; you would need to put: toggleList.forEach(fucntion (toggleList) {} because you’re changing .isOn for whatever was inside ()

I hope this helps a bit

1 Like

Try and talk yourself through what your code is doing. I think it might help a bit. Remember that we are trying to set all the toggles to on.

Thanks! I got it, deleted a few lines and changed scope of the function to the whole list. I guess i was making it to complicated.

That’s usually the case for me and sometimes I just delete everything and start again with pseudo code

1 Like

The ! operator will flip the value of isOn regardless of it’s initial value.
i did the same thing after not reading the instructions clearly :slight_smile: .
" The function will change the value of the isOn property to true for every switch in the list, and then return the updated array."

Haha its good to know that I wasn’t the only one who tried.
I went there because it says
“You can use the previous switchToggle function from the fourth challenge, however be careful, since we want all of them on, not toggled!”
so there must be a way to use the the expression to only toggle the value that is false.

1 Like

Yeah, I had coded the same, it was simpler. :sunglasses: But then I overthought :thinking: about which is quicker :balance_scale:, a variable attribution or a comparison.

performance - Which is faster? Comparison or assignment? - Stack Overflow

I may be wrong but this was not there when I did it when the challenge opened up so I think it was an addition later!

1 Like

surprised that I got stuck at when to return the toggleList. Pretty simple challenge.

I’m playing code golf with myself. Is there a more succinct way to do the return array?
Whenever I try to do things like => array, the compiler gets mad at me

Nice and simple loop to end the weekend. As others have mentioned, no need to complicate this with any comparisons, just the reassignment and final return.

Have a great Sunday folks!

2 Likes

The way I think about it is that the forEach function is looking at an individual item in the array it is operating on. In this case the item is an object and you want to set the isOn property of that object to true and then return the modified array.

Hmm, now that you mention it, yeah, just basically making each isOn = true works without the if-statement

isOn behaves strangely for me I printed it out and when I print console.log(toggleList[0].isOn) it gives true but in the object console.log(toggleList[0]) its false

const switchAllTogglesOn = (toggleList) => {
// Code here!
console.log(toggleList[0].isOn);
console.log(toggleList[0]);
for (let i of toggleList){
console.log(i);
console.log(i.isOn);
if (i.isOn ==true){
i.isOn=false;
}
else {
i.isOn=true;}
console.log(toggleList);
console.log(i.isOn);
return toggleList;
}
}

Thanks for confirming this! This is the first challenge I was able to solve reasonably quickly which surprised me so much that I spent 20 minutes trying to figure out if something was wrong lol

You literally just need to make EVERY toggle in the array to be true. That’s it. No need to check if the switch is off or not. Just cycle through all the items in the array and turn .isOn to be true for all items. There are soo many ways to do this one.

2 Likes

You don’t need to check if it is true. Ideally; just check if it is false. if(!i.isOn) is enough. And change it to true. You can delete all the rest. Hope it helps.

I mean I don’t think that makes things any easier the main problem is that i.isOn seems to return a different value than the value of isOn in the object i and I cannot figure out why.