How-To Guides

9.7.4 Leash CodeHS Answers: Concept, Step-by-Step Logic and Worked Solution

29 min read 0 Comments
A student working on the CodeHS Leash exercise on a laptop with a circle and line animation on screen

Quick answer: 9.7.4 Leash is a JavaScript graphics exercise in CodeHS, in the Drawing Lines lesson of the Introduction to Computer Science in JavaScript course. You create a ball (a circle) that follows the mouse pointer, and a line that connects the ball to a fixed anchor point at the centre of the canvas, like a dog on a leash. The solution has three parts: create the circle and the line once in start(), register a mouse-movement handler with mouseMoveMethod(moveLeash), and inside that handler move both the ball with ball.setPosition(mouseX, mouseY) and the line’s moving end with leashLine.setEndpoint(mouseX, mouseY). The line’s starting point never changes, which is what makes it look like a leash. The rest of this guide explains the concept behind each step, shows a worked solution with a line-by-line explanation, covers the mistakes almost every student makes, and explains how to test the program and what the exercise is really teaching you.

If you searched for “9.7.4 Leash CodeHS answers”, you are probably staring at a blank CodeHS editor wondering what a leash has to do with learning JavaScript. You are not alone. This exercise confuses thousands of beginners every semester, and the search for its answer spikes every single semester. The good news is that the concept behind it is simple, and once you understand it, the code is short. The bad news is that the exercise is designed to catch students who copy code without understanding it: the autograder tends to check behaviour rather than exact code, and a copied answer that does not match your assignment’s colours, sizes, or starter code can fail even when it looks right.

This guide is written the other way around. It teaches the concept first, then the logic, then a worked solution you can understand and adapt — not a block of code to paste blindly. It also flags the places where sources disagree about what the exercise asks for, because there is more than one version of the Leash problem in circulation. Some versions use a circle that follows the mouse with a line anchored at the centre of the canvas. Others describe a follower object that chases a leader in small steps. The version covered in detail here is the one most sources describe — the mouse-following ball with a line anchored at the centre — and the variations are covered separately so you can recognise them if your assignment is different. Always check the exact instructions in your own CodeHS editor, because your assignment’s colours, sizes, and grading checks take priority over any guide on the internet.

9.7.4 Leash at a Glance

Detail Information
Exercise 9.7.4 Leash
Platform CodeHS (codehs.com)
Course Introduction to Computer Science in JavaScript
Unit Animation and Games
Lesson 9.7, Drawing Lines
Language JavaScript, using CodeHS graphics
What you build A ball that follows the mouse, connected by a line to a fixed anchor
Key objects Circle, Line
Key methods mouseMoveMethod(), setPosition(), setEndpoint(), getWidth(), getHeight(), add()
Core skills Coordinates, mouse events, event-driven programming, updating existing objects
Common version Line anchored at canvas centre, ball follows the mouse
Other versions A follower object that chases a leader in small steps; a Dog/Leash object-relationship version in Java
What the grader checks Behaviour (ball follows, line stays attached) plus assignment-specific colours, sizes and names

What Is the 9.7.4 Leash Exercise in CodeHS?

CodeHS is an online platform used by schools to teach computer science, and one of its most popular courses is Introduction to Computer Science in JavaScript. Inside that course there is a unit called Animation and Games, which walks students through timers, mouse events, and drawing shapes on a graphics canvas. Lesson 9.7 is about drawing lines. Students watch a video example, complete a short quiz, look at an example program, and then arrive at Exercise 9.7.4, simply called Leash.

The exercise asks you to create a small animated scene on the graphics canvas. There are three parts to the scene, and understanding them separately is the key to the whole exercise:

  • The leader: a shape, usually a circle, that the user controls with the mouse. Move your mouse and the leader goes with it. In the most common version, this is a ball that follows the pointer exactly.
  • The follower: in some versions, a second shape that chases the leader, moving in small steps instead of teleporting. In the centre-anchored version, there is no follower — the anchor point itself plays the role of the thing the leash is attached to.
  • The leash: a line drawn from the anchor to the moving object, updated constantly so it always connects the two points. The line is what makes the scene look like a dog on a leash: one end is held still, the other end moves with the dog.

The central idea is simple: a line whose endpoints update every moment so it behaves like a flexible leash. The animation does not need a timer or a loop, because the mouse itself provides the updates. Every time the mouse moves, CodeHS calls your handler function, and your handler moves the ball and the line. When the mouse stops, the scene stops. That is event-driven programming in its simplest form, and it is the real lesson of the exercise.

Where the Exercise Sits in the Course

Lesson 9.7, Drawing Lines, comes after students have already learned to draw circles, rectangles, and other shapes, and after they have learned about mouse events. The exercise is a test of whether students can combine those two skills: drawing shapes and responding to the mouse. It is also a test of a subtler skill — knowing when to create an object and when to update an existing one — which is the mistake that trips up the most students, as the common-mistakes section below explains.

The exercise is short. A working solution is roughly twenty lines of code. But those twenty lines contain several ideas that beginners find genuinely hard: global variables, event handlers, and the difference between creating an object and moving one. That is why the exercise has a reputation for being confusing out of proportion to its length, and why so many students search for the answer instead of working it out.

Confirmed vs Claimed: Which Version Is Your Assignment?

Before writing any code, it is worth sorting out which version of the Leash exercise you actually have, because the sources describe at least three different versions, and they are not interchangeable.

Version Status What it asks for
Mouse-following ball, line anchored at canvas centre Most commonly described A circle follows the mouse; a line connects it to the centre; uses mouseMoveMethod and setEndpoint
Follower chases a leader in small steps Described by some guides A second shape moves toward the leader a few pixels at a time; uses a timer or repeated updates
Dog and Leash classes in Java (object relationships) Described by one guide A Dog class and a Leash class, constructors, passing objects as parameters
The exercise is in the JavaScript course, Drawing Lines lesson Confirmed dailycurrenthub.co.uk and domainagechecker.co.uk agree on the lesson placement
The exercise uses mouseMoveMethod, setEndpoint, getWidth/getHeight Confirmed Multiple guides describe the same CodeHS graphics methods
The ball is yellow, radius 20 Claimed, not verified One guide uses these values as an example; your assignment may specify different values
A timer or loop is required Disputed One guide says a timer runs the animation; the detailed guide says no timer is needed for mouse-following
Copying a published answer will pass the grader False Graders check behaviour and assignment-specific details; a copied answer that differs in colour, size, or names can fail
See also  BrumeBlog.com: What It Is, Who Writes for It, and Is It Legit?

The Java version deserves a special note. One guide (ityug247.com) describes 9.7.4 Leash as a Java exercise about classes — a Dog class, a Leash class, constructors, and objects passed as parameters. That is a real kind of CodeHS exercise, and it exists in the Java course, but it is a different lesson from the JavaScript graphics exercise that most searchers mean when they type “9.7.4 Leash CodeHS answers”. If your course is Introduction to Computer Science in JavaScript and your lesson is Drawing Lines, the Java description does not apply to you. If your course is Java and your assignment talks about classes and constructors, then the Java description is the right one, and the graphics solution in this guide is not. Check your course name and your starter code before you write anything. The two versions share a number and a name, and that is all they share.

The follower-chases-leader version is closer to the graphics version, and it may be the version your teacher assigned if your instructions mention a follower that moves gradually. The logic for that version is covered in its own section below, after the main solution. The core ideas are the same — coordinates, updating objects, and a line that connects two points — but the movement code is different, because the follower moves in steps rather than jumping to the mouse.

Simple illustration of the leash concept, a ball connected by a line to a fixed anchor point

The Concept Behind the Leash

The leash is a metaphor, and the metaphor does the teaching. Think of a real dog on a real leash. There is a hand holding one end, and a dog at the other end. The hand stays more or less still. The dog moves. The leash is the thing that connects them, and it stretches, shortens, and changes direction as the dog moves. It never disconnects, and it never moves on its own. Everything about the exercise follows from that picture.

In the CodeHS version, the hand is the anchor point — the centre of the canvas, in the most common version. The dog is the ball — a circle that follows your mouse. The leash is a line. And the program’s job is to keep the line connected to the ball at all times, no matter where the mouse goes.

That gives you the three jobs the program has to do:

  1. Create the objects once. The ball and the line are created when the program starts, and they are created only once. If you create a new ball every time the mouse moves, you will end up with hundreds of balls on the screen, which is one of the most common mistakes in this exercise.
  2. Listen for the mouse. The program registers a handler function with mouseMoveMethod, so that CodeHS calls that function every time the mouse moves over the canvas. The handler is the program’s ears: without it, the program has no idea the mouse has moved.
  3. Update the objects, don’t recreate them. Inside the handler, the program moves the existing ball to the mouse position with setPosition, and moves the line’s moving endpoint to the same position with setEndpoint. The line’s starting point is never touched, so it stays anchored.

The reason the ball and the line must receive the same coordinates is the whole point of the exercise. If the ball goes to (300, 150) and the line’s endpoint goes to (300, 150), the line ends exactly where the ball is, and the two look connected. If they go to different coordinates, even by a few pixels, the line will visibly detach from the ball, and the leash will look broken. That is why the handler reads the mouse position once and sends the same two numbers to both objects.

Coordinates: The Skill Underneath the Exercise

Graphics programming depends on accurately positioning objects, and the Leash exercise is really a coordinates exercise wearing a leash costume. The CodeHS canvas is a grid of pixels. The top-left corner is (0, 0). The x-coordinate increases as you move right, and the y-coordinate increases as you move down — which is the opposite of the maths graphs you are used to, where y increases upward. That trips up beginners, and it is worth stating plainly: on the CodeHS canvas, a larger y means lower on the screen.

The centre of the canvas is found with getWidth() / 2 and getHeight() / 2. getWidth() returns the width of the canvas in pixels, and dividing by two gives the horizontal middle. getHeight() does the same for the vertical middle. On a canvas measuring 400 pixels wide and 500 pixels high, the centre is at (200, 250). Calculating the centre this way, instead of typing in fixed numbers, means the program works on any canvas size. If your teacher changes the canvas dimensions, a program that uses getWidth() / 2 still finds the centre; a program that hard-codes (200, 250) does not.

The mouse position works the same way. When the mouse moves, CodeHS passes an event object to your handler function, and that object carries the mouse’s current coordinates. The methods e.getX() and e.getY() read them. Those two numbers are the only new information the handler receives, and everything the handler does is built from them.

The Worked Solution, Step by Step

Here is a complete worked solution for the most common version of the exercise, followed by a line-by-line explanation. This is a functional example built from the documented CodeHS graphics methods, as described by dailycurrenthub.co.uk — not a verified copy of the official answer key. Your assignment’s specific radius, colour, variable names, and starting positions take priority over this example. Use it to understand the structure, then adapt it to your instructions.

“`javascript var BALL_RADIUS = 20; var ball; var leashLine;

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

leashLine = new Line(centerX, centerY, centerX, centerY);
add(leashLine);

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

mouseMoveMethod(moveLeash);

}

function moveLeash(e) { var mouseX = e.getX(); var mouseY = e.getY(); ball.setPosition(mouseX, mouseY); leashLine.setEndpoint(mouseX, mouseY); } “`

Step 1: Understanding the Global Variables

The program begins by declaring three variables at the top, outside any function:

javascript var BALL_RADIUS = 20; var ball; var leashLine;

BALL_RADIUS controls the circle’s size. It is written in capital letters by convention, because it is a constant — a value that is set once and never changed while the program runs. Using a named constant instead of typing the number 20 in several places makes the program easier to change: if your assignment wants a radius of 30, you change one line.

ball and leashLine store references to the graphical objects. They are declared outside the functions because both the initialisation function (start) and the mouse-event handler (moveLeash) need access to the same objects. start() creates the ball and the line; moveLeash() moves them. If the ball were declared only inside start(), it would be a local variable, and moveLeash() would not be able to see it. That is a scope error, and it is one of the most common reasons a Leash program fails: the handler tries to move a ball it cannot reach, and nothing happens.

Declaring the variables at the top and assigning them inside start() is the standard pattern in CodeHS graphics programs. It is worth learning properly, because the same pattern appears in almost every animation exercise that follows.

Step 2: Finding the Centre of the Canvas

The first two lines inside start() calculate the centre:

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

These are local variables, used only inside start(), to hold the centre coordinates so they can be used for both the line and the ball. Calculating the centre dynamically, as explained above, avoids relying on fixed values that might not work for other canvas sizes.

Step 3: Creating the Line

javascript leashLine = new Line(centerX, centerY, centerX, centerY); add(leashLine);

The Line constructor takes four numbers: the x and y of the starting point, and the x and y of the ending point. In this example, the line starts and ends at the centre, so it initially has zero length — it is a single point, invisible on the canvas. That is deliberate. The line’s starting point will never change, and its ending point will be moved to the mouse position as soon as the mouse moves. Starting it at zero length means there is no stray line visible before the user moves the mouse.

See also  Carlos Alman: Cardi B's Father, Life, Family and Facts

The add() command displays the line on the graphics canvas. In CodeHS, creating an object does not automatically show it; you have to add it. Forgetting add() is a small mistake with a confusing symptom: the program runs, no errors appear, and nothing is visible.

Step 4: Creating the Circle

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

The circle is created with the Circle constructor, which takes the radius. setPosition() places the centre of the circle at the given coordinates — here, the centre of the canvas, so the ball starts at the anchor point and the line starts at zero length. setColor() sets the fill colour; this example uses yellow, but your assignment may specify a different colour, and the colour is one of the details the grader may check. The circle is added after the line, so it appears visually above the line where the two overlap. That drawing order gives the connection a cleaner appearance: the ball sits on top of the end of the leash, the way a real leash attaches to a collar.

Step 5: Registering Mouse Movement

javascript mouseMoveMethod(moveLeash);

This is the line that makes the program interactive. It tells CodeHS which function should respond to mouse movement. Notice that the function name is passed without parentheses. Writing moveLeash() instead would call the function immediately, once, and pass its result (which is nothing) as the handler — so the program would do nothing when the mouse moved. Passing moveLeash without parentheses passes the function itself, and CodeHS stores it and calls it later, every time the mouse moves. That distinction — passing a function versus calling it — is one of the most important ideas in event-driven programming, and this exercise is many students’ first encounter with it.

Whenever the mouse moves, CodeHS passes an event object to the handler, containing information about the pointer’s position. The program uses that information to update the graphical objects, as the next step explains.

Step 6: Updating the Ball and Leash

javascript function moveLeash(e) { var mouseX = e.getX(); var mouseY = e.getY(); ball.setPosition(mouseX, mouseY); leashLine.setEndpoint(mouseX, mouseY); }

Inside moveLeash(e), the methods e.getX() and e.getY() retrieve the mouse coordinates, and the program sends those values to two separate commands. ball.setPosition(mouseX, mouseY) moves the ball to the pointer’s location. leashLine.setEndpoint(mouseX, mouseY) moves the line’s ending point to the same position. Because both updates use the same coordinates, the line stays visually connected to the ball’s centre. That shared coordinate is the essential logic of the Leash exercise.

Note what setEndpoint does and does not do. It changes only the ending point of the line. The starting point — the anchor at the centre of the canvas — is untouched. That is what makes the line behave like a leash rather than a free-floating stick. The alternative method, move(), shifts the entire line, including both endpoints, which would drag the anchor along with the ball. That is a common mistake, covered in detail below.

Also note what is not in the handler: there is no new Circle, no new Line, and no add(). The handler only moves objects that already exist. Creating new objects inside the handler is the single most common error in this exercise, and it produces a very visible symptom — dozens or hundreds of circles trailing across the screen — which is covered in the mistakes section.

The Logic in Plain English

If the code above still feels abstract, here is the same program as a plain-English recipe. This is a good thing to write out on paper before you touch the editor, because it forces you to think through the logic instead of copying syntax.

Step Action Purpose
1 Declare the ball and line variables at the top Both functions need to reach the same objects
2 Calculate the centre of the canvas Find the anchor point dynamically
3 Create a line from the centre to the centre The leash starts at zero length, anchored
4 Create a circle at the centre The ball starts at the anchor
5 Register the mouse handler Tell CodeHS to call your function on mouse movement
6 On each mouse move, read the mouse position Get the new target coordinates
7 Move the ball to the mouse position The ball follows the pointer
8 Move the line’s endpoint to the same position The leash stays attached to the ball

And here is the same logic as a mental picture. Imagine the canvas as a table. Put your finger on the centre of the table and keep it there — that is the anchor. Now put a coin on the table and slide it around with your other hand — that is the ball, following the mouse. Now imagine a piece of string connecting your finger to the coin. As the coin moves, the string stretches and changes direction, but your finger never moves. That string is the line, and the program’s job is to redraw it every time the coin moves. If you let go of the string and tie a new one every time the coin moves, you will end up with a pile of string on the table. That is exactly what happens when a program creates a new line inside the mouse handler.

The Follower Version: When the Leash Has a Chasing Dog

Some versions of the exercise add a second moving object: a follower that chases the leader instead of teleporting to the mouse. The bcorner.net guide describes this version. In it, one object (the leader) moves freely — usually with the mouse — and a second object (the follower) tracks it, moving toward it step by step. The leash connects the two moving objects instead of connecting a moving object to a fixed anchor.

The logic is different in one important way. The follower does not jump to the leader’s position. It checks where the leader is, compares that with its own position, and moves a small amount closer. The basic flow is:

  1. Get the leader’s position (x, y).
  2. Compare it with the follower’s position.
  3. Move the follower slightly closer.
  4. Repeat continuously.

In code, that looks like a comparison on each axis:

  • If the follower is to the left of the leader, move it right.
  • If the follower is to the right of the leader, move it left.
  • If the follower is above the leader, move it down.
  • If the follower is below the leader, move it up.

The movement happens inside a timer function, which runs many times per second, so the follower glides toward the leader instead of jumping. The leash in this version is a line whose both endpoints update: one endpoint follows the leader, the other follows the follower. That is more work than the centre-anchored version, and it introduces a new problem — how far should the follower move in each step? Move it too far and it overshoots and jitters; move it too little and it never catches up. A common choice is a few pixels per timer tick, which gives a smooth trailing effect, like a dog that is always a step behind its owner.

If your assignment describes a follower that chases, this is the version you have. If your assignment describes a line anchored at the centre, use the main solution above. The two versions are not interchangeable, and the timer-based follower logic will not pass a grader that expects the centre-anchored version, or the other way around. Read your instructions and match the version to them.

Common Mistakes in 9.7.4 Leash (and How to Fix Them)

Almost every student who struggles with Leash makes one of five mistakes. They are listed here with their symptoms, because the symptom is usually how you discover the mistake.

The Ball Does Not Move

Symptom: The ball sits at the centre of the canvas no matter where you move the mouse. The line may or may not move with it.

Causes: The mouse-event function was not registered correctly, or the callback cannot access the ball variable. Check whether mouseMoveMethod(moveLeash); appears in start(). Also confirm that the handler receives the event parameter (the e in moveLeash(e)) and uses e.getX() and e.getY(). If the ball was declared inside start() instead of at the top of the program, the handler cannot see it — move the declaration to the global scope. JavaScript is case-sensitive, so check the spelling of every method name: mouseMoveMethod, setPosition, and setEndpoint must match the documented names exactly.

See also  Tophillsport.com: What It Is, What It Offers, and Whether You Can Trust It

The Line Does Not Follow the Ball

Symptom: The circle follows the pointer, but the line stays in its original position, or stretches from the wrong place.

Cause: The event handler updates the circle but does not update the line. Both objects need to receive the same coordinates. Make sure the handler contains both ball.setPosition(mouseX, mouseY) and leashLine.setEndpoint(mouseX, mouseY), and that both use the same two variables. Using setEndpoint() is especially important, because it changes the ending point while preserving the fixed anchor.

Multiple Circles Appear

Symptom: A trail of circles spreads across the canvas as you move the mouse, like a snail trail.

Cause: A new Circle object is created inside the mouse-movement function, so every mouse movement adds another circle. The fix is to create the circle once in start() and store a reference to it, then use setPosition() to move the existing circle. The same principle applies to the line: create it once, update it with setEndpoint(), and never create a new one inside the handler. Reusing graphical objects makes the behaviour easier to control and is the single most important habit this exercise teaches.

The Entire Line Moves

Symptom: The line moves as a whole, with both ends sliding across the canvas, instead of staying anchored at one end.

Cause: The handler uses move() instead of setEndpoint(). The move() method shifts the entire line, including both endpoints, by a given amount. That is the wrong tool for an anchored leash. The starting point should remain stationary while the second endpoint changes, and setEndpoint() is the method that does that.

The Program Looks Correct but Does Not Pass

Symptom: The canvas shows exactly what you expect — the ball follows the mouse, the line stays attached — but the grader marks it wrong.

Causes: A visually correct program can still fail if its implementation differs from the assignment’s specific requirements. Check the expected circle size, colours, starting positions, function names, and any starter code CodeHS supplies. If your assignment says the ball should be red with a radius of 25, a yellow ball with a radius of 20 will look right and fail the check. If your assignment provides starter code with specific variable names, use those names. An example solution from the internet cannot guarantee full marks without the exact exercise instructions and grading checks, and this is the most common reason a copied answer fails. Compare your work with the instructions in your own CodeHS editor before submitting, and change the example’s values to match.

A hand moving a computer mouse while testing a graphics program on a laptop

How to Test the Leash Program

Testing is simple, and you should do it in a specific order so that each test tells you something different.

  1. Run the program and look at the starting state. The ball should appear at the centre of the canvas, and the line should be invisible or a single point, because it starts at zero length. If the ball is not at the centre, check the starting position in start(). If nothing is visible at all, check that both objects were added with add().
  2. Move the mouse slowly to the right. The ball should follow, and the line should extend from the centre toward the ball. The line’s starting point should not move. If the whole line slides, you are using move() instead of setEndpoint().
  3. Move the mouse up, down, and diagonally. The line should change direction and length while its starting point remains fixed. If the line detaches from the ball, check that both updates use the same mouse coordinates.
  4. Move the mouse back toward the centre. As the ball approaches the anchor, the visible line should become shorter, and when the ball reaches the centre, the line should shrink to a point again.
  5. Move the mouse quickly in a circle. The ball should track the pointer smoothly, and there should be exactly one ball on the screen. If you see a trail of circles, a new circle is being created inside the handler — move the creation into start().
  6. Check the details against your assignment. Compare the ball’s colour, radius, and starting position with the instructions. If the grader checks a specific colour and yours is different, fix the colour before submitting.

The line in the basic example is not restricted to a maximum length — it will stretch as far as the mouse goes. Creating a physically limited leash, one that stops the ball from going too far from the anchor, would require additional distance calculations and movement constraints, and it is not part of the standard exercise. If your assignment asks for a limited leash, that is an extension, and it needs a distance check inside the handler: calculate the distance between the ball and the anchor, and if it exceeds the limit, stop the ball at the limit instead of moving it to the mouse.

Why the Leash Exercise Matters

The Leash exercise looks trivial once you understand it, but it teaches four ideas that appear again and again in programming, and that is why it is in the course.

Understanding Event-Driven Programming

Most programs beginners write run from top to bottom and then stop. Event-driven programs are different: they wait. They register functions that run in response to actions — mouse movement, keyboard input, button clicks, timers — and they do nothing until an event happens. The Leash exercise demonstrates this with a compact visual example. Rather than running a loop to check the pointer position repeatedly, the program registers a function that CodeHS calls when a mouse movement event occurs. That is more efficient, and it is how almost all interactive software works, from web pages to games to mobile apps.

Developing Coordinate Skills

Graphics programming depends on accurately positioning objects, and the Leash exercise is a concentrated dose of coordinate practice. Students learn how horizontal and vertical coordinates control movement, why the y-axis points downward on a screen, and why multiple graphical objects sometimes need to share the same position. Those concepts become useful in every animation, drawing application, and interactive game that follows.

Learning to Update Existing Objects

A valuable programming habit is understanding when to create an object and when to modify an existing one. The Leash exercise creates its graphical objects during initialisation, and the mouse handler then changes their positions without repeatedly constructing new shapes. Separating object creation from object updates keeps programs understandable, prevents unnecessary graphics from accumulating on the screen, and avoids wasting memory. The trail-of-circles mistake is the lesson that teaches this habit, and it is a lesson most students remember.

Thinking in Terms of Relationships Between Objects

The leash is a relationship: two objects, connected by a rule. The ball moves, and the line follows, because the program enforces the relationship on every mouse movement. That idea — that a program maintains relationships between objects, updating one when another changes — is the foundation of animation, of user interfaces, and of the object-oriented programming that comes later in the course. The Java version of this exercise makes that idea explicit, with a Dog class and a Leash class that hold references to each other. The JavaScript version teaches the same idea visually, with a line that refuses to let go of its ball.

Frequently Asked Questions About 9.7.4 Leash CodeHS Answers

What is 9.7.4 Leash in CodeHS?

It is a JavaScript graphics exercise in the Drawing Lines lesson (Lesson 9.7) of the CodeHS Introduction to Computer Science in JavaScript course. You create a ball that follows the mouse and a line that connects it to a fixed anchor point, like a dog on a leash.

What is the main objective of the Leash exercise?

The commonly described objective is to make a circle follow the mouse while a line connects the moving circle to a fixed point on the canvas. It teaches coordinates, mouse events, event-driven programming, and the difference between creating an object and updating one.

Which programming language is used for 9.7.4 Leash?

The exercise appears in a JavaScript course and uses CodeHS graphics functions such as mouseMoveMethod, setPosition, and setEndpoint. A separate Java version about Dog and Leash classes also exists, so check your course name before writing code.

What does mouseMoveMethod() do?

It registers a callback function that runs whenever the mouse moves over the graphics canvas. You pass the function name without parentheses, for example mouseMoveMethod(moveLeash);, and CodeHS calls that function with an event object each time the mouse moves.

Why is setEndpoint() important in the solution?

The setEndpoint() method changes the ending point of a line without changing its starting point, which keeps the line anchored at the centre of the canvas. The alternative, move(), shifts the whole line, which would drag the anchor along with the ball.

Why should the ball and line variables be declared outside start()?

Declaring them in the global scope allows both the initialisation function and the mouse-event handler to access the same graphical objects. If they were declared only inside start(), the handler could not reach them, and the ball would not move.

Why might my CodeHS Leash answer fail the grader?

Possible causes include incorrect coordinates, a missing event registration, variable-scope errors, a wrong colour or radius, or differences between the submitted code and the assignment’s specific requirements. A visually correct program can still fail if it does not match the details in your own CodeHS editor.

Does the Leash exercise require a timer or a continuous loop?

Not for the mouse-following version. The registered mouse-event handler performs the necessary updates whenever CodeHS reports mouse movement, so no timer is needed. The follower-chases-leader version does use a timer, because the follower has to keep moving even when the mouse is still.

Related Reading

scookietechie

Writes about technology, software and the trends shaping the digital world for ScookieTechie.

Leave a Reply

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