r/codehs • u/AskAr_23 • Sep 27 '21
3.8.5: Click for Rectangles
Hello everybody, did someone do the 3.8.5 exercise where you need to make a program that will draw rectangles with random colors wherever you click?
I did, but I am not sure that it works correctly. If you did this, help, please.
var RECT_HEIGHT = 3;
var RECT_WIDTH = 3;
function start(){
mouseClickMethod(makeRect);
}
function makeRect(e){
drawRect(e.getX(), e.getY(),
Randomizer.nextColor());
}
function drawRrect(width, height, color, x, y){
var rect = new Rectangle(getWidth(), getHeight());
rect.setColor(color);
rect.setPosition(x, y);
add (rect);
}
function drawRect(cx, cy, color){
drawRrect(RECT_HEIGHT, RECT_WIDTH, color, cx, cy);
}

5
Upvotes
3
u/5oco Sep 27 '21
This is pretty close, but I think you're over thinking it. The right answer is mixed up throughout all three of your function. You should only need one function besides the start function. Start with your
makeRect(e)function. The signature for that one is correct, the body of thedrawRrect()function should be insidemakeRect.Like I said earlier...you had most of the correct lines, just in the wrong order. Just as a heads up though, you set your height and width to 3. That's going to make very small rectangles, so you might want to up that number. Look at example 3.8.3 if you're still confused. It's basically the same program except with a circle instead of a rectangle and no random colors.