9.7.4 Leash CodeHS Answers: The Full Code — 3 Myths and 1 Costly Mistake
If you are stuck on 9.7.4 Leash in CodeHS, the exercise looks simple and then refuses to work: the ball sits still, the line points nowhere, or the screen fills with circles. This guide gives you the 9.7.4 leash codehs answers with the full JavaScript code, explains every line in plain language, and shows the mistakes that stop most students. The exercise is a beginner graphics task in the CodeHS Introduction to Computer Science in JavaScript course, in the Drawing Lines lesson. A yellow circle follows your mouse pointer, and a black line stays connected to it, anchored at the centre of the canvas — like a dog on a leash.
The Short Answer
The 9.7.4 leash codehs answers come down to two functions and two objects. In start(), you create a circle and a line once, place both at the centre of the canvas, and register a mouse movement handler with mouseMoveMethod(). In the handler, you read the mouse position with e.getX() and e.getY(), move the circle there with setPosition(), and stretch the line to the same point with setEndpoint(). The line’s other end never moves, so it stays anchored at the centre while the ball follows your pointer.
var BALL_RADIUS = 30;
var ball;
var line;
function start() {
var centerX = getWidth() / 2;
var centerY = getHeight() / 2;
line = new Line(centerX, centerY, centerX, centerY);
line.setColor(Color.black);
add(line);
ball = new Circle(BALL_RADIUS);
ball.setPosition(centerX, centerY);
ball.setColor(Color.yellow);
add(ball);
mouseMoveMethod(leash);
}
function leash(e) {
var mouseX = e.getX();
var mouseY = e.getY();
ball.setPosition(mouseX, mouseY);
line.setEndpoint(mouseX, mouseY);
}
That is the whole program. The rest of this guide explains why each line is there, what changes between course versions, and the one mistake that breaks everything.
What Is 9.7.4 Leash in CodeHS?
Leash is a JavaScript graphics exercise in CodeHS, a coding platform used by schools to teach computer science. The exercise sits in the Drawing Lines lesson of the Introduction to Computer Science in JavaScript course, in the Animation and Games section. The numbers in “9.7.4” are just an address inside the course: they tell you where the exercise sits in the curriculum. They are not a JavaScript command and not a special setting, and the number can differ across CodeHS curriculum versions, so your classroom instructions are the best reference for your own assignment.
The task behind the 9.7.4 leash codehs answers is to build a small interactive drawing. A circle — the ball — starts at the centre of the canvas. A line starts at the same centre point. When you move your mouse, the ball follows the pointer, and the line stretches from its fixed centre anchor to the ball. Picture a kite string: one end is in your hand, the other end is on the kite.
| Quick Facts | |
|---|---|
| Exercise | 9.7.4 Leash |
| Platform | CodeHS |
| Language | JavaScript with the CodeHS Graphics library |
| Course | Introduction to Computer Science in JavaScript |
| Lesson | Drawing Lines |
| Section | Animation and Games |
| Objects | Circle and Line |
| Main event method | mouseMoveMethod() |
| Difficulty | Beginner |
| Main learning goal | Update graphics using mouse coordinates |
The exercise practises three things at once: making shapes, listening for the mouse, and changing shapes that already exist. That last one is the real lesson. Beginners usually want to draw something new every time the mouse moves. Leash teaches the opposite: make the objects once, then move them.
The Complete 9.7.4 Leash Code

The code in the Short Answer is the standard solution, and it is worth reading slowly before you run it. It uses the version most guides describe: a ball with a radius of 30, a yellow circle, a black line. This is the program the 9.7.4 leash codehs answers are built on. One common variant uses a radius of 20 and a line width of 3, with the handler named moveLeash instead of leash. The structure is identical in both. If your assignment asks for a different radius, colour or line width, change those values and keep everything else the same.
A note on what the code does not do: there is no distance limit, no timer and no loop. The ball follows the mouse directly, wherever it goes. Some “answers” pages show a different program — a follower that only moves when the distance grows past a limit, using Math.sqrt and a leashLength variable. That is a different problem, not this exercise, and pasting it in will not match the assignment. It is covered as Myth 1 below.
How the Code Works in 6 Steps

The program has two functions, and MDN’s Functions guide explains the general idea: a function is defined once and called whenever it is needed. Here is what each part of this program does.
Step 1: Declare the variables outside the functions
var BALL_RADIUS = 30;
var ball;
var line;
The two variables ball and line are declared at the top, outside both functions, so both functions can reach them. start() will store the objects in them, and leash() will move them later. If you declared them inside start() instead, the handler could not see them, and the program would fail with an undefined variable error. Keeping the radius in one named variable, BALL_RADIUS, also makes it easy to change the size in one place.
Step 2: Find the centre of the canvas
var centerX = getWidth() / 2;
var centerY = getHeight() / 2;
getWidth() gives the canvas width and getHeight() gives its height. Dividing each by two gives the centre. On a 400-by-400 canvas, the centre is (200, 200). Computing it instead of typing a fixed number means the program still works if the canvas size changes. In CodeHS graphics, X grows to the right and Y grows downward from the upper-left corner, so the centre is the same point no matter which corner you think of first.
Step 3: Create the line first
line = new Line(centerX, centerY, centerX, centerY);
line.setColor(Color.black);
add(line);
new Line() takes four numbers: the starting X and Y, then the ending X and Y. Here both ends start at the centre, so at first the line has no length at all and you cannot see it. Move the mouse and it appears. That is how it is supposed to look at the start. setColor(Color.black) colours it black, and add(line) puts it on the canvas — making the object is not enough on its own. The line is created before the ball so the ball is drawn on top of it.
Step 4: Create the circle
ball = new Circle(BALL_RADIUS);
ball.setPosition(centerX, centerY);
ball.setColor(Color.yellow);
add(ball);
new Circle(BALL_RADIUS) makes a circle with a radius of 30, which means a diameter of 60. setPosition() places the centre of the circle at the given coordinates — you do not need to subtract the radius, because the method already works from the centre. Then the colour is set to yellow and add(ball) displays it. The circle is created only once, here in start(), and never again.
Step 5: Register the mouse handler
mouseMoveMethod(leash);
This line connects mouse movement to the leash function. Whenever the pointer moves over the canvas, the graphics system calls leash and hands it a mouse event. Note the function name has no parentheses after it: you are passing the function itself, not calling it. If you wrote mouseMoveMethod(leash()), the function would run once immediately and nothing would be registered. This is the line students forget most often, and a forgotten handler is the most common reason the ball never moves.
Step 6: Move the ball and stretch the line
function leash(e) {
var mouseX = e.getX();
var mouseY = e.getY();
ball.setPosition(mouseX, mouseY);
line.setEndpoint(mouseX, mouseY);
}
In the 9.7.4 leash codehs answers, the handler receives the mouse event as e. e.getX() and e.getY() give the pointer’s current coordinates, stored here in mouseX and mouseY. Then two lines do the work: ball.setPosition(mouseX, mouseY) moves the circle to the pointer, and line.setEndpoint(mouseX, mouseY) moves only the line’s far end to the same point. The line’s starting point is never touched, so it stays anchored at the centre. Because both updates use the same two numbers, the ball and the line always meet exactly, with no visible gap.
The 3 Myths About 9.7.4 Leash CodeHS Answers
Myth 1: The answer is the distance-limit follower code
Some pages that rank for 9.7.4 leash codehs answers show a program where a follower only moves when the distance passes a limit, using Math.sqrt(dx * dx + dy * dy) and a leashLength variable, then creeping forward by a fraction of the gap. That is a real programming pattern, but it is a different exercise. The CodeHS Leash exercise has no distance limit and no creeping movement: the ball jumps straight to the mouse, and the line follows it. If you paste the distance version in, the ball will lag behind the pointer and the checker will fail.
Myth 2: The numbers are the same for everyone
The radius, the colours and the line width change between course versions. One widely copied version uses a radius of 30; another uses 20 and adds setLineWidth(3). Some versions name the handler moveLeash instead of leash, and some name the line variable leash instead of line. None of these is “the” answer. Only your own assignment sheet tells you which numbers your checker wants, so read it before you submit.
Myth 3: You have to subtract the radius from the mouse position
Beginners often assume the circle will sit off-centre from the pointer and try to correct it with mouseX - BALL_RADIUS. You do not need to. In CodeHS JavaScript Graphics, setPosition() places the centre of a circle at the coordinates you give it, so the ball’s centre lands exactly on the pointer. Subtracting the radius would push the ball up and left of the cursor instead.
The 1 Costly Mistake: Creating New Objects Inside the Mouse Handler

The single most damaging mistake is also the easiest to make. Instead of creating the circle once in start() and moving it, a student writes new Circle() inside the mouse handler, or new Line() there, or both. Every mouse movement then adds another object to the canvas. Move the pointer across the screen once and you have hundreds of circles and lines stacked on top of each other. The program slows down, the canvas turns into a trail of shapes, and the checker fails even though the ball appears to follow the mouse.
The fix is the core idea of the 9.7.4 leash codehs answers exercise: create once, update forever. start() builds the objects. The handler only changes their positions. If your program leaves a trail behind the ball, or if it gets slower the longer you move the mouse, look inside your handler first — a new keyword there is almost certainly the cause.
The same rule shows up in every interactive program you will write later, from games to drawing tools, which is why this small exercise exists at all. The same habit applies to reading about the field around it. If you want context on where this kind of interactive graphics work leads, the LLM News and TechTrendery explainers on this site cover the technology news and blog side of the same world — though neither is a coding tutorial, so treat them as background, not as the assignment.
6 Common Errors and Their Fixes
| Problem | Likely cause | Fix |
|---|---|---|
| Ball does not move | Mouse handler never registered | Check mouseMoveMethod(leash) is called in start() |
| Line does not follow | Endpoint never updates | Check line.setEndpoint(mouseX, mouseY) inside the handler |
| Nothing appears | Objects never added | Check add(line) and add(ball) |
| Error about an undefined variable | Wrong scope | Declare ball and line outside the functions |
| Hundreds of circles appear | Objects created during movement | Create objects once in start(), only update positions in the handler |
| Program looks right but does not pass | Details do not match the assignment | Recheck radius, colours, line width and function names against your instructions |
Two more small traps are worth knowing. JavaScript is case-sensitive, so getx() is not getX() and setposition() is not setPosition() — a single wrong capital letter breaks the program. And a gap between the ball and the line usually means the two updates use different numbers: compare ball.setPosition() with line.setEndpoint() and make sure their arguments match.
How to Test Your Leash Program
To test the 9.7.4 leash codehs answers code, run the program and first check the starting picture: a yellow circle at the centre of the canvas, with no line visible yet. The invisible line is expected — both of its ends start at the same point.
Then move the pointer slowly to each corner of the canvas. The ball should follow it, and the line should stretch from the centre to the ball without a gap. Try quick turns, slow movements and diagonal paths. Watch that the fixed end of the line stays at the centre and that no extra shapes appear behind the ball. If the ball lags, jumps or leaves a trail, the cause is in the table above.
Finally, compare your 9.7.4 leash codehs answers code with the assignment instructions one more time, line by line: the radius, the colours, the line width, the object names and the handler name. A program can look perfect and still fail a checker because one detail does not match.
Frequently Asked Questions
What is 9.7.4 leash codehs answers?
That is the name students use for worked solutions to the Leash exercise — the yellow circle, the black line, and the mouse code that connects them.
What is 9.7.4 Leash in CodeHS?
It is an exercise in the Drawing Lines lesson of the Introduction to Computer Science in JavaScript course, in the Animation and Games section. The numbers identify the exercise inside the course structure, and they can differ across CodeHS curriculum versions, so your classroom instructions are the best reference.
Which programming language is used for 9.7.4 Leash?
JavaScript, using the CodeHS Graphics library. The code runs in the CodeHS editor in the browser, with no installation needed.
How do you make the ball follow the mouse?
Tell the program to watch the mouse with mouseMoveMethod(), then in the handler take the pointer’s coordinates from the event and hand them to the circle with setPosition(). The handler runs every time the mouse moves, so the ball tracks the pointer.
What does setEndpoint() do?
It moves the far end of a line to new coordinates and leaves the near end exactly where it was. In Leash, it moves the far end to the mouse position while the centre end stays fixed.
Why is my 9.7.4 Leash code not working?
The usual causes behind broken 9.7.4 leash codehs answers are an unregistered mouse handler, a missing add() statement, a variable declared in the wrong scope, objects created inside the handler instead of once in start(), or a detail that does not match your assignment, such as the radius or the colours.
Should I copy the 9.7.4 leash codehs answers?
Read the example until you can explain each line, then type your own version from that understanding and run it. Copying without understanding has two problems: different course versions ask for different details, so a copied solution may not pass your assignment, and you miss the methods — mouse events, variable scope, updating objects instead of creating them — that the next exercises will ask for again. Follow your school’s rules about using outside help.
The Bottom Line
The 9.7.4 leash codehs answers are short: two objects made once in start(), one mouse handler, and two updates inside it. The ball follows the pointer because setPosition() gets the mouse coordinates, and the line follows the ball because setEndpoint() gets the same two numbers. The anchor never moves, the objects are never recreated, and the details — radius, colours, names — come from your own assignment. Learn those five facts and the exercise is done; learn why they work and the next ten exercises get easier too.
