9.7.4 Leash CodeHS Answers: Easy JavaScript Solution and Complete Step-by-Step Guide
Learn How to Complete the CodeHS Leash Exercise, Understand Mouse Events, and Fix Common Programming Mistakes
Learning JavaScript becomes more interesting when students can see their code respond to real movements. The 9.7.4 Leash CodeHS Answers exercise introduces this experience through a simple interactive graphics program. Students create a ball connected to a fixed point by a line, then make the ball follow the mouse pointer.
Although the activity appears straightforward, it combines several essential programming concepts, including coordinates, graphical objects, event handling, and functions. Understanding how these elements interact makes the exercise easier and prepares you for more advanced animation projects.
This article explains the Leash exercise, provides an example solution, explores important JavaScript commands, and highlights common mistakes students should avoid.
What Is the 9.7.4 Leash CodeHS Exercise?
The Leash exercise is part of the Drawing Lines lesson in the CodeHS Introduction to Computer Science in JavaScript curriculum.
Its central goal is to create an interactive connection between a circle and a fixed point on the graphics canvas.
The program includes two graphical elements: a circle representing the ball and a line representing the leash.
One endpoint of the Line remains at the center of the canvas. The other endpoint follows the ball as the user moves the mouse.
The result resembles a ball attached to a flexible leash. Students learn how separate graphical objects can move together while maintaining their intended relationship.
Understanding How the Leash Program Works
The program runs on mouse-movement events rather than a continuous animation timer.
When the program starts, the Circle and Line are created and positioned at the center of the canvas.
As the mouse moves, CodeHS provides updated horizontal and vertical coordinates. The program uses those values to reposition the Circle and update the Line’s movable endpoint.
Important JavaScript Concepts
Several methods make the exercise possible.
- Circle(): Creates a circular graphical object.
- Line(): Creates a line between two coordinate points.
- setPosition(): Changes a graphical object’s position.
- setEndpoint(): Updates the ending point of a line.
- mouseMoveMethod(): Registers a function that responds to mouse movement.
- getX() and getY(): Retrieve the horizontal and vertical mouse coordinates from an event.
Together, these commands produce the interactive leash effect.
9.7.4 Leash CodeHS Answers: Example JavaScript Solution
The following example demonstrates the standard graphics approach used for this exercise.
var BALL_RADIUS = 25;
var ball;
var leashLine;
function start() {
var centerX = getWidth() / 2;
var centerY = getHeight() / 2;
leashLine = new Line(
centerX, centerY,
centerX, centerY
);
leashLine.setColor(Color.black);
add(leashLine);
ball = new Circle(BALL_RADIUS);
ball.setPosition(centerX, centerY);
ball.setColor(Color.blue);
add(ball);
mouseMoveMethod(moveLeash);
}
function moveLeash(e) {
var x = e.getX();
var y = e.getY();
ball.setPosition(x, y);
leashLine.setEndpoint(x, y);
}
This example follows the documented CodeHS JavaScript graphics methods. Students should compare it with their assigned exercise instructions because required variable names, colors, or object sizes may differ.
Step-by-Step Explanation of the Code
Step 1: Declare the Variables
The program begins by defining the ball’s radius and declaring variables for the graphical objects.
Declaring ball and leashLine outside the functions allows both the starting function and mouse-event handler to access them.
Step 2: Calculate the Canvas Center
The commands getWidth() and getHeight() return the canvas dimensions.
Dividing each value by two identifies its horizontal and vertical center.
This approach works across different canvas sizes without requiring fixed coordinate values.
Step 3: Create the Line and Ball
The Line () constructor creates a line whose endpoints initially share the center coordinates.
The Circle() constructor creates the ball, and setPosition() places it at the same location.
Adding the Line before the Circle also helps keep the ball visually in front of the leash.
Step 4: Register the Mouse Event
The statement mouseMoveMethod(moveLeash); instructs CodeHS to call the specified function whenever the mouse moves.
The function name is passed without parentheses because the program must register a callback rather than execute the function immediately.
Step 5: Update Both Objects
How the Mouse Coordinates Control Movement
The event parameter e contains mouse movement information.
The commands e.getX() and e.getY() retrieve the pointer’s current coordinates.
These values update the Circle and line endpoint simultaneously. The Line’s starting point remains unchanged, creating the desired leash behavior.
Common Problems and How to Fix Them
Even a short graphics program can produce unexpected results.
The Ball Does Not Move
Check whether mouseMoveMethod() has been registered correctly. Also verify that the event function uses the proper circle variable.
The Line Does Not Follow the Ball
This usually happens when the program updates the Circle but forgets to change the Line’s endpoint.
Use setEndpoint() with the same coordinates assigned to the ball.
The Line Moves Completely
Calling move() on the Line shifts both endpoints. The Leash exercise requires one endpoint to remain fixed, so updating only the ending point is more appropriate.
The Program Reports an Undefined Variable
Make sure the circle and line variables are accessible inside the event function.
Also confirm that the graphical objects are created before the mouse handler attempts to modify them.
How to Test Your CodeHS Leash Solution
Testing confirms that the program behaves correctly, not just that it runs without errors.
Start by running the program and checking that the ball appears at the center of the canvas.
Next, move the mouse toward different corners. The ball should follow the pointer, and the leash should stretch from its fixed starting point.
Move the pointer horizontally, vertically, and diagonally to verify that both graphical objects stay synchronized.
Finally, compare the result with the requirements displayed in your CodeHS assignment. A successful visual result may still need adjustments to satisfy particular automated checks.
Why This Exercise Matters for Beginners
The Leash exercise teaches more than drawing a circle and a line.
It introduces event-driven programming, where a program responds to user actions instead of following only a fixed sequence.
Students also gain experience with object references, coordinate systems, function parameters, and updating existing graphics.
These skills are valuable when building drawing applications, interactive menus, simple games, and mouse-controlled animations.
Understanding the relationship between the two graphical objects also encourages students to organize their code logically.
Frequently Asked Questions
1. What is the main purpose of CodeHS 9.7.4 Leash?
The exercise teaches students to combine JavaScript graphics with mouse movement events. Its main objective is to move a ball while keeping it visually connected to a fixed point through a line.
2. Which programming language is used in the Leash exercise?
The commonly listed 9.7.4 exercise uses JavaScript with the CodeHS graphics library. Students work with graphical objects, coordinates, functions, and mouse events.
3. Why does my CodeHS leash line not move?
The most likely reason is that the mouse-event handler does not update the line endpoint. Check that setEndpoint() receives the current mouse coordinates.
4. Do I need a loop for the Leash exercise?
A loop is unnecessary for the basic mouse-following version. The mouse movement callback automatically executes whenever the pointer changes position over the canvas.
5. Can I change the ball’s color or size?
Yes. The setColor() method changes its appearance, while the radius provided to Circle () determines its size. Follow any specific requirements given by your teacher.
6. What is the difference between setPosition() and setEndpoint()?
The setPosition() method changes an object’s position. For a line, setEndpoint() specifically changes its ending point without moving its starting point.
Conclusion
The 9.7.4 Leash CodeHS Answers exercise offers a practical introduction to interactive JavaScript graphics. By combining a circle, a line, and a mouse-movement handler, students create a responsive visual program with relatively little code.
The key to completing the activity is understanding how mouse coordinates control the ball and the endpoint of its connected Line. With careful testing and a clear understanding of the underlying methods, students can complete the exercise and apply the same techniques to future CodeHS projects.



