We managed to push some updates and clarifications for this challenge. Here's a walkthrough of how I arrived at the solution. Hopefully that will help to see the problem-solving process. We worked through all the great feedback from the original thread about this challenge and pushed some fixes to cut down on confusion. Part of the issue is that this is where the course starts to get pretty difficult and all the concepts are really coming together simultaneously.
Challenge Description
Starter Code
You don't get much to work with at the start, but thankfully the description is pretty verbose so it should give a good idea of where to go.
var totalGen = 19;
var totalMW = 0;
Walkthrough
The first thing that jumps out in the challenge is that we'll need to construct a for
loop. And that it will have to contain conditionals for everything we need in the challenge. So I started by scaffolding out a blank for loop:
var totalGen = 19;
var totalMW = 0;
for (var count = 1; count <= totalGen; count++) {
}
The count
variable isn't very clear though, and since it's all the generators that we're looping through I changed the name accordingly:
var totalGen = 19;
var totalMW = 0;
for (var currentGen = 1; currentGen <= totalGen; currentGen++) {
}
Conditionals
Now the challenge states that we're basically working through all the generators and only dealing with even numbered ones that should be turned on:
var totalGen = 19;
var totalMW = 0;
for (var currentGen = 1; currentGen <= totalGen; currentGen++) {
if (currentGen % 2 == 0) {
currentGen = "on";
}
}
Note that we aren't actually doing this at the end of the challenge, it's just an easy way to mentally start scaffolding out which generators will be on and off.
Now that we have the even numbered generators that means that all the rest are the odd numbered generators that are going to be set to off:
var totalGen = 19;
var totalMW = 0;
for (var currentGen = 1; currentGen <= totalGen; currentGen++) {
if (currentGen % 2 == 0) {
currentGen = "on";
} else {
currentGen = "off";
}
}
Now I can replace those temporary values with the console.log()
statements with the correct format that the challenge asks for:
var totalGen = 19;
var totalMW = 0;
for (var currentGen = 1; currentGen <= totalGen; currentGen++) {
if (currentGen % 2 == 0) {
console.log("Generator #" + currentGen + " is on, adding 62 MW, for a total of " + totalMW + " MW!");
} else {
console.log("Generator #" + currentGen + " is off.");
}
}
Output
At this point it seems like we've done enough work to see what the output would look like if we just run the loop.
Generator #1 is off.
Generator #2 is on, adding 62 MW, for a total of 0 MW!
Generator #3 is off.
Generator #4 is on, adding 62 MW, for a total of 0 MW!
Generator #5 is off.
Generator #6 is on, adding 62 MW, for a total of 0 MW!
Generator #7 is off.
Generator #8 is on, adding 62 MW, for a total of 0 MW!
Generator #9 is off.
Generator #10 is on, adding 62 MW, for a total of 0 MW!
Generator #11 is off.
Generator #12 is on, adding 62 MW, for a total of 0 MW!
Generator #13 is off.
Generator #14 is on, adding 62 MW, for a total of 0 MW!
Generator #15 is off.
Generator #16 is on, adding 62 MW, for a total of 0 MW!
Generator #17 is off.
Generator #18 is on, adding 62 MW, for a total of 0 MW!
Generator #19 is off.
The first thing that jumps out is that our totalMW
is never moving. And that we're only dealing with the first four generators where the output is 62 MW
. So we'll have to start adding to the totalMW
value and also accounting for the 124 MW
generators.
totalMW
This way we're now adding to the totalMW
value with each iteration of the loop:
var totalGen = 19;
var totalMW = 0;
for (var currentGen = 1; currentGen <= totalGen; currentGen++) {
if (currentGen % 2 == 0) {
totalMW += 62;
console.log("Generator #" + currentGen + " is on, adding 62 MW, for a total of " + totalMW + " MW!");
} else {
console.log("Generator #" + currentGen + " is off.");
}
}
Difficulty
This is where things tend to get a little tricky. Now we need to account for generators 5
through 19
. So my first thought was to add a new conditional:
var totalGen = 19;
var totalMW = 0;
for (var currentGen = 1; currentGen <= totalGen; currentGen++) {
if (currentGen % 2 == 0) {
totalMW += 62;
console.log("Generator #" + currentGen + " is on, adding 62 MW, for a total of " + totalMW + " MW!");
} else if ( some condition here ) {
totalMW += 124;
console.log("Generator #" + currentGen + " is on, adding 124 MW, for a total of " + totalMW + " MW!");
} else {
console.log("Generator #" + currentGen + " is off.");
}
}
You can see that the log statements look okay with that code. And the totalMW
will get incremented. But the conditions are difficult to meet since we're dealing with two things:
- Whether the
currentGen
is even or odd.
- Whether the
currentGen
is in the first four or the latter fifteen.
Solution
In order to handle the complex conditional statements we can add logical &&
statements to take care of the rest:
var totalGen = 19;
var totalMW = 0;
for (var currentGen = 1; currentGen <= totalGen; currentGen++) {
if (currentGen % 2 == 0 && currentGen <= 4) {
totalMW += 62;
console.log("Generator #" + currentGen + " is on, adding 62 MW, for a total of " + totalMW + " MW!");
} else if (currentGen % 2 == 0 && currentGen >= 5) {
totalMW += 124;
console.log("Generator #" + currentGen + " is on, adding 124 MW, for a total of " + totalMW + " MW!");
} else {
console.log("Generator #" + currentGen + " is off.");
}
}
I could see how this could be pretty difficult if you're still just getting started with JavaScript. But ultimately we're really combining all the concepts that we've learned in the Road Trip in this one challenge. It's difficult, but doable.
Explanation
The conditionals for that solution basically break things down into the possible scenarios. The else
statement covers any odd numbered generators that should be set to off.
The if
statement (currentGen % 2 == 0 && currentGen <= 4
) covers all the even numbered generators in the first four:
if (currentGen % 2 == 0 && currentGen <= 4) {
totalMW += 62;
console.log("Generator #" + currentGen + " is on, adding 62 MW, for a total of " + totalMW + " MW!");
}
And the else if
(currentGen % 2 == 0 && currentGen >= 5
) covers the even numbered generators from 5 to 19:
else if (currentGen % 2 == 0 && currentGen >= 5) {
totalMW += 124;
console.log("Generator #" + currentGen + " is on, adding 124 MW, for a total of " + totalMW + " MW!");
}
Updates and Clarification
I really hope these challenge walkthroughs are helpful. In addition to working through how the challenge works I think it helps me to contextualize where all the confusion is coming from.
Thanks to everyone for all your feedback. If you think of any particular way that we can improve this challenge feel free to post your recommendation and we'll be happy to hear about it.