Blog

9.7.4 Leash CodeHS Answers: Complete JavaScript Solution

A yellow ball and a black line can teach students how to track mouse movement in JavaScript. The 9.7.4 Leash exercise on CodeHS uses these two shapes to teach interactive graphics. It is worth five points and appears in the Drawing Lines lesson.

The 9.7.4 leash codehs answers use global variables, canvas coordinates, and mouse events to control both shapes. This guide provides the complete JavaScript code, explains each function, and helps students fix common errors.

What Is 9.7.4 Leash on CodeHS?

Leash is a beginner coding task on CodeHS. It is part of the Introduction to Computer Science in JavaScript course. Both the Golden and Bulldog course versions include this exercise. It is worth five points.

The task teaches students how to control shapes with a mouse. Students create a circle and a line on a canvas. They use JavaScript functions to change their positions.

The exercise also teaches event-driven programming. The program runs a function when the user moves the mouse.

9.7.4 Leash CodeHS Answers: Exercise Requirements

Students must create a yellow circle with a radius of 30 pixels. The circle starts at the center of the canvas. A black line also starts and ends at this point.

The yellow circle must follow the mouse as it moves. The end of the black line must follow the circle. However, the starting point must stay at the center.

The result looks like a ball tied to a leash. The line changes its length and direction as the mouse moves.

9.7.4 Leash CodeHS Answers: Complete JavaScript Code

The following code uses the built-in CodeHS Graphics library. It creates both shapes and uses one function to track mouse movement.

var BALL_RADIUS = 30;
var ball;
var line;

function start() {
    var centerX = getWidth() / 2;
    var centerY = getHeight() / 2;

    // Create the black line
    line = new Line(centerX, centerY, centerX, centerY);
    line.setColor(Color.black);
    add(line);

    // Create the yellow ball
    ball = new Circle(BALL_RADIUS);
    ball.setPosition(centerX, centerY);
    ball.setColor(Color.yellow);
    add(ball);

    // Listen for mouse movement
    mouseMoveMethod(moveLeash);
}

function moveLeash(e) {
    line.setEndpoint(e.getX(), e.getY());
    ball.setPosition(e.getX(), e.getY());
}

The code creates each shape only once. It updates the shapes when the mouse moves. This stops extra shapes from appearing on the screen.

Students do not need to install extra tools or packages. CodeHS provides all the required graphics functions. The code meets the described task rules but has not been tested with the private CodeHS autograder.

How the 9.7.4 Leash CodeHS Answers Work?

The program starts with the start() function. It finds the center of the canvas. It then creates the black line and yellow circle.

At first, the line has no visible length. Both ends share the same point. The yellow circle stays at the center until the mouse moves.

The moveLeash() function tracks the mouse. It updates both shapes with the new mouse position. The ball moves while the start of the line stays fixed.

Understanding Global Variables in CodeHS Leash

Global variables are declared outside functions. Different functions can use and change them. This exercise uses global variables to store the ball and line.

var BALL_RADIUS = 30;
var ball;
var line;

The start() function creates the graphics objects. The moveLeash() function uses the same objects to change their positions.

Adding var before ball or line inside start() creates local variables. The global variables remain undefined. This may cause a TypeError when the mouse moves.

How 9.7.4 Leash CodeHS Answers Use Canvas Coordinates?

CodeHS uses X and Y coordinates to place shapes. The top-left corner starts at (0, 0). The X value grows to the right. The Y value grows downward.

The getWidth() function returns the canvas width. The getHeight() function returns its height. Dividing both values by two gives the center.

var centerX = getWidth() / 2;
var centerY = getHeight() / 2;

For example, a canvas that is 400 pixels wide and 500 pixels tall has a center at (200, 250). This method works with different canvas sizes.

Creating the Yellow Circle in CodeHS Leash

The Circle() constructor creates the ball. The BALL_RADIUS variable sets its radius to 30 pixels. The full width of the circle is 60 pixels.

ball = new Circle(BALL_RADIUS);
ball.setPosition(centerX, centerY);
ball.setColor(Color.yellow);
add(ball);

The setPosition() method places the ball at the canvas center. The setColor() method makes it yellow. The add(ball) method displays it on the screen.

The program uses the same circle each time the mouse moves. It changes its position instead of creating a new circle.

Creating the Black Line in CodeHS Leash

The Line() constructor uses four values to create a line. The first two values set its starting point. The last two values set its endpoint.

line = new Line(centerX, centerY, centerX, centerY);
line.setColor(Color.black);
add(line);

Both ends start at the canvas center. The line has zero length at first. The setEndpoint() method changes its length and direction.

The program adds the line before the circle. This keeps the yellow ball above the black line. It stops the line from covering the center of the ball.

How 9.7.4 Leash CodeHS Answers Handle Mouse Events?

Mouse events help programs respond to mouse actions. CodeHS uses the mouseMoveMethod() function to track mouse movement. It calls a selected function each time the mouse moves.

9.7.4 Leash CodeHS Answers
mouseMoveMethod(moveLeash);

The moveLeash() function receives an event object called e. The e.getX() method gets the mouse’s X coordinate. The e.getY() method gets its Y coordinate.

function moveLeash(e) {
    line.setEndpoint(e.getX(), e.getY());
    ball.setPosition(e.getX(), e.getY());
}

Both shapes use the same coordinates. This keeps the ball attached to the end of the line. The program does not need a timer or loop.

Common Errors in 9.7.4 Leash CodeHS Answers

A common mistake is declaring the ball and line again inside start(). This leaves the global variables undefined. Another mistake is using mouseMoveMethod(moveLeash()). The extra parentheses call the function too early.

Some students use the wrong method to move the line. The line.setPosition() method changes its starting point. Use line.setEndpoint() to move its endpoint instead.

Common ErrorSolution
Ball does not moveCheck the mouse event function.
Line does not stretchUse line.setEndpoint().
Shapes do not appearUse the add() method.
Extra circles appearCreate the circle outside the mouse event function.
Undefined object errorCheck the global variables.
Line covers the ballAdd the line first.
Mouse events failPass the function without parentheses.
Starting point movesUse line.setEndpoint().

Use one mouse event function to update both shapes. This makes the code easier to read and fix.

How to Test and Submit 9.7.4 Leash CodeHS Answers?

Log in to CodeHS and open your JavaScript course. Find the Animation and Games module. Open the Drawing Lines lesson and select the Leash exercise. Enter the code in the editor.

Run the program and check the yellow ball. It should start at the center. Move the mouse across the canvas. The ball should follow the mouse while the line stays tied to the center.

Check for errors before you submit the code. Make sure the ball has the right size and color. Use the grading or submission controls to finish the task. These controls may differ between course versions.

Final Thoughts

The CodeHS Leash exercise teaches basic JavaScript graphics. Students learn how to create shapes, use coordinates, and track mouse movement. They also learn about global variables and drawing order.

These skills are useful for building games, drawing tools, and drag-and-drop features. Learning how each function works can help students solve more coding tasks.

FAQs

What are the 9.7.4 Leash CodeHS answers?

The solution uses a yellow circle, a black line, and mouseMoveMethod() to track the mouse.

What is the ball radius in CodeHS Leash?

The yellow ball has a radius of 30 pixels and a diameter of 60 pixels.

Why is my CodeHS Leash line not moving?

Check the mouse event function and use line.setEndpoint() to move the line.

Does the CodeHS Leash exercise require a timer?

No, the program uses mouse events instead of a timer or loop.

Can I run CodeHS Leash code outside CodeHS?

Not directly, as the code needs the CodeHS JavaScript Graphics library.


Read Also: Is MataRecycler Real? Facts About the Smart Recycling Concept

Carlos Odea

Carlos Odea is a writer at The Cincinnati Magazine. He covers celebrity news, entertainment, lifestyle, and pop culture. His interest in media and communications helps him explore the latest trends and stories from the entertainment world. Outside of writing, Carlos enjoys keeping up with movies, television, music, and celebrity updates.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button