If you searched for leash codehs answers, you probably have a ball, a line, and a mouse pointer that refuses to cooperate. This guide explains what exercise 9.7.4 is really testing, the three ideas that make the program work, and a fill-in outline you can finish in a few minutes. It stops short of a paste-ready solution on purpose, partly because of how CodeHS reviews student work and partly because the answer pages that rank for this search disagree with each other about what the exercise even is. By the end you should be able to write the program on your own and explain every line of it.
What the Leash exercise asks you to build
According to the CodeHS course page, Leash is activity 9.7.4 inside lesson 9.7, Drawing Lines, in the Animation and Games unit of Introduction to Computer Science in JavaScript (Golden). It sits right after the Drawing Lines practice exercise (9.7.3) and right before the Advanced Animator Badge, and the lesson before it covers mouse-moved events. The picture you are making is simple: a circle follows your mouse pointer, and a line, the leash, stretches from a fixed anchor point to the circle.
The full instructions live inside the CodeHS editor behind a login, so they are not publicly readable. Details such as the ball color, its size, and exactly where the leash is anchored are therefore best taken from the instructions on your own screen. Several answer sites state those details with great confidence, but none of them can be checked against a public CodeHS page, and a wrong assumption about the anchor point is an easy way to fail a check you could have passed.
The three ideas behind every working version
The first idea is to create your shapes once and change them many times. The start() function runs a single time when the program begins, so that is the place to build the circle, build the line, and add both to the canvas. The function that reacts to the mouse runs again every time the pointer moves, which means it should only modify shapes that already exist. If you build a new circle inside that function, every tiny mouse movement stamps another ball onto the canvas and you end up with a smeared trail instead of one ball.
The second idea is that the mouse event hands you the coordinates. When you register a function with the mouse-move handler, CodeHS passes it an event object, usually named e, and e.getX() and e.getY() return where the pointer is. The CodeHS lesson on drawing lines teaches this exact pattern one step earlier. It creates a zero-length line when the mouse goes down and then, while the mouse is dragged, updates the line’s endpoint with the pointer’s coordinates. Leash is the same pattern attached to a different event, with a circle added to the update.
The third idea is knowing how each shape is positioned. The CodeHS documentation says a circle’s setPosition places its center, so you do not need to subtract the radius to line the ball up under the pointer. A line has two ends: setPosition changes its starting point, while setEndpoint(x2, y2) changes the far end. A leash that stays anchored while the ball moves needs only one of those two methods in the update, and picking the wrong one drags the whole leash around with the mouse.
One smaller rule from the same CodeHS lesson is easy to miss. If the circle or line is declared with let or var inside start(), the mouse function cannot see it, because the variable is local to start(). Declare the variables above both functions so they are shared.

A fill-in outline instead of a copy-paste answer
Here is the structure with the thinking left for you. Each comment describes a job, and the method names to look up are Circle, Line, add, mouseMoveMethod, setPosition, and setEndpoint.
let ball; // shared so the mouse function can see it
let leashLine; // shared for the same reason
function start() {
// 1. Create the circle, set its color, add it to the canvas
// 2. Decide where the leash is anchored (check your instructions),
// then create a Line whose two ends both begin at that spot
// 3. Add the line to the canvas
// 4. Register your mouse function with mouseMoveMethod
// (pass the function's name only, without parentheses)
}
function followMouse(e) {
// Move the ball's center to the pointer using e.getX() and e.getY()
// Move only the far end of the line to the same point
}
Fill in those six comments with real code, run it, and wiggle the mouse. If the ball tracks the pointer and the line stays pinned at one end while the other end chases the ball, you have solved the exercise by understanding it, which is also the version you can defend if a teacher asks you to explain your code.
Why your Leash program is misbehaving
Most failed attempts fall into a handful of patterns, and each one has a visible symptom that points to the cause.
| What you see | Likely cause | What to change |
|---|---|---|
| Nothing happens when you move the mouse | The handler was never registered, or it was registered with parentheses so it ran once and returned nothing | Pass the function name to mouseMoveMethod without () |
| A trail of balls fills the canvas | The circle is being created inside the mouse function | Create it in start() and only reposition it afterward |
| The line never appears | It was created but not added, or it was drawn underneath the ball | Call add on the line and check the order you add shapes |
| The whole leash slides with the mouse | setPosition was used on the line, which moves its start point | Use setEndpoint and leave the start alone |
A red error mentioning setEndpoint | The method received something other than two finite numbers | Check both arguments, and make sure calls like getWidth() keep their parentheses |
| The ball sits slightly off from the cursor | A radius offset was subtracted from a position that already refers to the center | Pass the pointer coordinates directly |
| An error saying a variable is undefined | The shape was declared inside start() | Move the declaration outside every function |
The error in the fifth row comes straight from the CodeHS graphics library, which refuses endpoint values that are not finite numbers and hints that a missing set of parentheses on getWidth() or getHeight() is a common culprit.

Why copied Leash CodeHS answers are a weak plan
Searching for this exercise turns up pages that contradict one another. One describes a canvas program with a mouse-driven ball and line, while another describes creating two classes for a dog and its leash. I could not find any CodeHS source for a class-based version, and it does not fit lesson 9.7 of the JavaScript Golden course, so a student who copies the wrong one may submit code that solves a different assignment. Some pages are clearly auto-generated filler that never shows a single line of code, and others only repeat a heading about answers, pricing, and buyer’s guides that has nothing to do with programming.
There is also the question of what your teacher can see. According to CodeHS’s own help pages, teachers on CodeHS Pro get a plagiarism report that shows similarity to other students’ code, total time spent, and the number of saves, and they can replay a student’s work through Code History. CodeHS notes that a complex exercise finished quickly with very few runs or saves is a signal worth a closer look. A 2026 CodeHS update also mentions paste detection in Code History and a Similarity Matrix that compares every pair of students on an assignment. These are signals for a teacher to investigate rather than automatic verdicts, but a program that appears fully formed after one paste is exactly the pattern they are designed to surface.
The practical lesson is that Leash is a short exercise. Writing it yourself takes about as long as hunting for a trustworthy copy, and what you learn here, creating objects once and updating them from an event, returns in the Key Events lesson and in the Crazy Ball Game that follow later in the unit.

Quick answers to questions students ask about Leash
Why is nothing visible until I move the mouse?
The mouse function only runs when the pointer moves. If both ends of the line start at the same point, the leash has zero length and cannot be seen until the first movement stretches it, which is expected behavior and not a bug. If the ball should show up immediately, give it a starting position in start().
Is 9.7.4 the same exercise number in every CodeHS course?
No. Exercise numbers belong to a specific course, and the 9.7.4 label above comes from the JavaScript Golden course page. I could not confirm whether a Leash exercise with the same behavior exists in the Python or Java courses, so check the course name at the top of your own assignment before following any guide.
What if I follow the outline and I am still stuck?
Go back to the 9.7.1 example and the earlier mouse-moved lesson, then add a console.log of e.getX() and e.getY() inside your mouse function to confirm it fires and receives real numbers. If the numbers print and the shapes still do not move, the problem is in how you update them. If your teacher allows it, a quick question about where the leash is anchored is better than guessing.
The short version
Leash CodeHS answers are easy to find and hard to trust, but the program behind them is only a few lines built on three ideas: create shapes once in start(), read the pointer position from the event, and move the ball’s center and the line’s endpoint together. Work through the outline above, use the symptom table when something looks wrong, and you will have a version that is yours and that you can explain on request.
EXPLORE MORE: Michigan Man $1M Lottery Win: Recent Winners, What They Really Take Home, and How the Claim Works

Written by Hassan, founder and editor of NTX Magazine — an independent news and biography publication. Every article is researched and cross-checked against multiple public sources before publishing; see our Sourcing Standards page for details.




