Challenge 4 Megathread

Welcome to challenge #4!

Use this thread for any and all questions relating to challenge 4.

1 Like

Looks like challenge 4 has a typo in the second input example? True remains true.

No it is not typo, because in the instructions say “Using the function twice should revert the toggle back to its original value.”.

You need to return the opposite of isOn so when ran twice it returns to original value

oh lol my brain skipped over the comments :skull:

1 Like

I seem to be having an issue, no matter what the initial value is in my code, it always return false…

but my code should set the opposite of the initial value, no?


const switchToggle = (toggle) => {
let toggle1 = false;
    toggle1 = !toggle1;
      return (toggle)
}

Your code won’t work because it’s not using toggle.isOn. If you use .isOn you should be able to figure it out from there. Make sure to take a look at example input / output so you can see what your working with. Good luck!

They give you the initial value of isOn, so I dont think you need to initialize it (the let line).

Sorry I just can’t find it anywhere, where can I find my points? I don’t have a team, and how does pointing system work if solo. Thank you

1 Like

No need to define a toggle (toggle1) as the toggle. In the parameter of the function is the object you want to play with. You can use toggle.name or toggle.isOn to access the values. Think of it like a switch, if you call the command swichToggle, you want the opposite of the current state it is in to be used. You can set this by using the toggle.isOn = [The state] and then return the toggle object.

1 Like

Hey, I got an email this morning saying that I missed yesterday’s coding challenge, but I completed challenge 3 yesterday and I can see my code and the discuss button. I also have access to challenge 4 too

Good morning!

You’re all good, it’s because you completed the third challenge after the emails were scheduled to be sent, no worries!

Oh okay great! Thanks!

Is there any other place to get challenges like this? Aside from learning about coding, I just find it fun and would love to have more puzzles to do rather than waiting for a new one each day…

1 Like

RIght, so this would be better according to your message, if I get it right (lol).


const switchToggle = (toggle) => {
    toggle.isOn = false;
    toggle.isOn = !toggle.isOn;
      return (toggle)
}

This would technically return true but the UI shows nothing, the section where it shows expected and returned is not present.

I’m assuming something is wrong with what I coded, but I can’t put my finger on it.

There is a value for isOn already. No need to set it.

1 Like

Oh well, so close yet so far. Thanks, I appreciate the help :grinning_face_with_smiling_eyes:

Happy to help! I’m finding I enjoy these!

When I update the value by using toggle.isOn=value it adds a second isOn property to the toggle object instead of updating. I found a workaround by deleting the isON property and then adding the new one but that can’t be the “correct” solution. Any tips on where I went wrong?

You initialized another toggle, toggle1, to be false, then you switched toggle1 to true. However, toggle1 has not related to the given toggle. Therefore, your code always returns the original toggle (name and states) no matter what you did to toggle1.

To fix this, you need to

  • find a way to connect your toggle1 to the given toggle
  • OR directly change the given toggle state.