Challenge 13 Megathread

Welcome to challenge 13! Use this thread for any and all questions relating to challenge 13.

image

2 Likes

Here is my solution:

const lightsOn = function(lights) {
lights.forEach(light=>light.on=true);
return lights;
};

const lightsOff = function(lights) {
lights.forEach(light=>light.on=false);
return lights;
};

const toggleLights = function(lights, lightsAreOn) {
lightsAreOn ? lights.forEach(light=>light.on=false) : lights.forEach(light=>light.on=true);
return lights;
};
1 Like

Simple solution with no error checking but passes all test cases

const lightsOn = function(lights) {
// Code here!
lights.forEach(light =>{light.on = true});
return lights;
}

const lightsOff = function(lights) {
lights.forEach(light =>{light.on = false});
return lights;
}

const toggleLights = function(lights, lightsAreOn) {
// Code here!
if(lightsAreOn)
{
return lightsOff(lights);
}

return lightsOn(lights);
}

the other way:

const lightsOn = lights => toggleLights(lights, false);
const lightsOff = lights => toggleLights(lights, true);

const toggleLights = (lights, lightsAreOn) => lights
  .map(light => ({...light, on: !lightsAreOn}))

4 Likes

This challenge is a perfect one to look into more advanced Array methods like Array.map() if you want (hint: Array.map() itself returns an array, neat!)

Also Object.assign is something you might want to research, it’s nice for changing the value of a key in an object :wink:

1 Like

I’m looking for some help understanding the logic in the last function. I originally wrote this and read it as, if lightsAreOn (true) equals true) run the function from earlier that turns lights off… I don’t understand why I have to add return function and the (lights) in brackets. Thanks!!

const toggleLights = function(lights, lightsAreOn) {
if (lightsAreOn === true) {
lightsOff;
}
else if (lightsAreOn === false) {
lightsOn;
}
return lights;
};

you need to pass your lightsoff and lightsOn function some variable,
so use lightsOff(lights); and lightsOn(lights); instead

1 Like

Did anyone use !light.on to flip the boolean without a test or is that not frequently done in javascript?

const lightsOn = function(lights) {
  for (var light of lights){
    light.on = true;
  }
  return lights;
}

const lightsOff = function(lights) {
  for (var light of lights){
    light.on = false;
  }
  return lights;
}

const toggleLights = function(lights, lightsAreOn) {
  for(var light of lights){
    light.on = !light.on;
  }
  return lights;
}
1 Like

My solution:

11%20AM

Both the lightsOff and lightsOn functions take one argument, called lights. So when you call these function you need to give it the values.

toggleLights (not so) coincidentally has two arguments: lights and lightsAreOn. The lights in this case are the lights you need to pass to the lightsOn/lightsOff functions.

In code:

const toggleLights = function(lights, lightsAreOn) {

  // 'lights' here is the array of all the lights. We need to 'feed'
  // this to the 'lightsOff' function (otherwise it won't know what to do).

  if (lightsAreOn === true) {

    // Here we pas 'lights' from 'toggleLights' down to 'lightsOff'.
    // But wait, that function returns something, so we need to
    // store the value that it returns as well!

    let lights = lightsOff(lights); 
  }

  // Ahhhh, now we can return lights again (after 'lightsOff' has
  // manipulated them to all be off).
  
  return lights
}

Does this explanation make sense?

2 Likes

That’s a very elegant way of doing it actually!

foo = !foo is a great way to toggle between true/false.

However, keep in mind that (in your case), you’re not actually doing what the challenge has asked you to do (which is to use the lightsOn and lightsOff methods from within toggleLights).

It may “work”, because the tests that are written are not complete, but in practice this will no turn all lights on or off, it will switch their on state individually instead.

2 Likes
const lightsOn = lights => lights.map(({ id }) => ({ id, on: true }));

const lightsOff = lights => lights.map(({ id }) => ({ id, on: false }));

const toggleLights = (lights, lightsAreOn) =>
  lightsAreOn ? lightsOff(lights) : lightsOn(lights);
2 Likes

I am not sure how the true and false is passed in some of the solutions above. I added the true and false and it passed but still not sure if it is correct or why it works if it is not. I am also not sure why when I add console.log before return it doesn’t produce output.

const toggleLights = function(lights, lightsAreOn) {
// Code here!
if(lightsAreOn ===true)
{
return lightsOff(lights, false);
}
else
return lightsOn(lights, true);
};

No need to pass true/false to lightsOn or lightsOff, they only take 1 argument (lights), nothing else.

lightsOn(lights, true, false, 0, 1) would not use anything besides the lights variable.

Thanks for the explanation! I see why it’s necessary.

1 Like

Wow I guess I skimmed the instructions a little too quickly.

const toggleLights = function(lights, lightsAreOn) {
  if(lightsAreOn){
    lightsOff(lights);
  }else{
    lightsOn(lights);
  }
  return lights;
}

Ohhh, you used a map for the first 2 functions. I had something similar but a bit more wordier.

My solution
const lightsOn = (lights) => {
    for (let [i, v] of Object.entries(lights)) {
      lights[i].on = true;
    }
    return lights;
};

const lightsOff = (lights) => {
    for (let [i, v] of Object.entries(lights)) {
      lights[i].on = false;
    }
    return lights;
};

const toggleLights = (lights, lightsAreOn) => {
    return lightsAreOn ? lightsOff(lights) : lightsOn(lights);
};