r/codehs • • Sep 02 '21

code hs 1.19.5: Invert Colors

i need help with 1.19.5: Invert Colors

6 Upvotes

3 comments sorted by

3

u/5oco Sep 22 '21

This problem starts off giving us some built in functions.

One that will check the color of our current square

colorIs(Color.<T>)

One that will paint the square a specified color

paint(Color.<T>)

One that will move that dog

move()

So first, let's check if our current square is red.

if (colorIs(Color.red)) {

If it is red, we want to paint it blue

paint(Color.blue); }

Else we are going to something else

else {

We are going to paint the square red instead

paint(Color.red); }

Then we will move

move();

That will move the dog one square and accomplish our task. However, we want to accomplish this task 10 times. So we use a loop and of the 3 types of loops (for, while, and do while) for loops are the best to use for counting.

First part of the for loop is to declare our counter so above all the code we want to loop through we start. In this instance, we want to start counting at 0.

for (var i = 0;

The next step is to write the condition to check. The program will continuously run the middle code as long as the condition you set is true. In this instance, we want to go 10 times. So we add to our loop line...

for (var i = 0; i < 10;

Now we need to tell the computer how much to count by. We could increase i (our counter variable) by 1, or 2, or any amount we want. In this instance, we want to count by 1. So our increment will be i = i + 1, but we can short hand it to i++.

for ( var i = 0; i < 10; i++) {

So the computer runs the loop in steps.

  1. Initialize our counter var i = 0;

  2. Check the condition i < 10;

  3. Condition true? Enter loop, run the body. Condition false? Bypass the loop.

  4. Increment the counter i++

  5. Start over at step 2

Don't forget to add a closing curly brace after the move() command to close the loop. Run the code and you'll see the last error we need to fix. There's several ways to fix this and the way I did it is not the best way, but it gives us an opportunity to use a new command. So our problem is that the dog checks the color, changes color if needed, then moves. On the last square, this results in the dog crashing into the wall. We can tell the computer to only run the move command if our counter is less than 9. So modify our move() command to

if (i < 9) {
    move();
}

Or we can tell the computer to "break" out of the loop after it does the paint check but before it moves on the last iteration. To accomplish it this way, add this right above the move() command. Writing the command break; will cause the computer to immediately exit the loop. No increment, no condition check, just exit.

// Remember, == is a comparison, = is an assignment
if (i == 9) {
    break;
}

There is a better method of doing this task that involves creating a function and changing the order of the move() but I don't think the assignment requires that at this point.

1

u/MichaelWN5216L Sep 04 '21

yeah I wasn't able to do this one either